Part B: Strings, Lists, Tuples, Loops, Conditionals, File I/O
Exercises
Each exercise asks you to write a (short) Python program. Comment
your code blocks and use descriptive variable names.
- Write a program GetGA.py that uses the URL
http://csc.ucdavis.edu/~chaos/courses/clab/Homework/GettysburgAddress.txt
to grab a text file containing the Gettysburg Address.
Have the program write this to a file named ga.txt.
- Write a program WordLineCounts.py that counts the number of lines
and words in ga.txt, printing the resulting counts to the terminal.
Annotate the output so that it is understandable:
Count statistics in file ga.txt
The number of words is ###.
The number of lines is ###.
- Write a program VowelCounts.py that reads in ga.txt
and counts the number of each kind of vowel, printing the results for each
vowel to the terminal. Annotate the output so that it is understandable.
- Write a program SortFile.py that reads in and alphabetically
sorts the lines (not individual words) in ga.txt and outputs those lines to a new file
ga_sorted.txt.
- Iterate a chaotic one-dimensional map: Write a program
OneDMapIterate.py that iterates
the function f(x) = 4.0 x ( 1 - x ). First, set the initial
condition x = 0.3. Then apply f(x) repeatedly to each new x.
Iterate for 100 steps, printing on each line the iteration number and
the successive x values to file onedmap.txt.
- Analyze a chaotic one-dimensional map: Write a program
OneDMapAnalyze.py that reads the
data in onedmap.txt. Calculate the mean value of the iterates,
printing the result to the terminal; again annotate the output so that
it is understandable. Determine the fraction of iterates that have x > 0.5.
Also, print out that result.
Table of Contents