This lab assignment requires you to write a few programs in Python. First, run update21. This will create the cs21/labs/02 directory and copy over any starting-point files for your programs. Next, move into your cs21/labs/02 directory and begin working on the Python programs for this lab. The pwd command helps you verify that you are in the correct sub-directory.
$ update21 $ cd cs21/labs/02 $ pwd /home/your_user_name/cs21/labs/02We will only grade files submitted by handin21 in this directory, so make sure your programs are in this directory!
As you write your first programs, start using good programming practices now:
Write a program called funstr.py that asks the user for a text string, and then displays the string in the format shown below (vertically, with the first half on the left side of the line, and the second half on the right side).
$ python funstr.py text: swarthmore s| w| a| r| t| |h |m |o |r |e
$ python funstr.py text: 12345 1| 2| |3 |4 |5
Write a program called diagonal.py that asks the user for a text string, and then displays the string on a diagonal, three letters at a time (see below).
$ python diagonal.py text: ABCDE ABC BCD CDE
$ python diagonal.py text: we love comp sci!! we e l lo lov ove ve e c co com omp mp p s sc sci ci! i!!You can assume that all inputs from the user have at least 3 characters. (If the user types in fewer than 3 characters, don't worry about what your program does.)
Ask the user for the number of letters to display on each line, instead of always showing 3.
$ python diagonal.py text: abcde n: 2 ab bc cd de
Write a program, called series.py, that estimates the sum of the following infinite series:
$$ 1 + {1 \over 2^2} + {1 \over 3^2} + {1 \over 4^2} + \ldots $$
We obviously can't sum all the (infinite) terms, so your program should ask for the number of terms to include, and then calculate the sum including that many terms. For example, if the user enters 4, your program will just sum the first 4 terms shown above:
$ python series.py number of terms: 4 1: 1.00000 2: 1.25000 3: 1.36111 4: 1.42361
The above output shows the total running sum on the right, after adding that many terms (on the left). So 1.25000 is the sum of the first two terms, and 1.36111 is the sum of the first three terms, and so on.
Here's another example, shortened to fit on the page:
$ python series.py number of terms: 300 1: 1.00000 2: 1.25000 3: 1.36111 4: 1.42361 5: 1.46361 6: 1.49139 7: 1.51180 ... ... 295: 1.64155 296: 1.64156 297: 1.64157 298: 1.64158 299: 1.64160 300: 1.64161
If you use enough terms, the sum should eventually converge on 1.64493.
Euler showed the above series sum to be ${\pi^2 \over 6}$. Use the math library and the above program to calculate pi, showing the value for pi as you add more and more terms to the sum.
$ python series.py number of terms: 300 1: 1.00000 ... so pi = 2.44949 2: 1.25000 ... so pi = 2.73861 3: 1.36111 ... so pi = 2.85774 4: 1.42361 ... so pi = 2.92261 ... ... 298: 1.64158 ... so pi = 3.13839 299: 1.64160 ... so pi = 3.13840 300: 1.64161 ... so pi = 3.13841
Write a program called reading.py that could help Swarthmore students keep track of a reading deadline. Your program should ask the student how many pages they have to read and how many days they have left. The program should then ask for the amount they read each day (just a fake simulation), keeping track of how many pages and days are left.
Here's a quick example:
$ python reading.py pages to read? 100 days until due? 5 Pages for day 1: 20 You've read 20 out of 100 pages. ** You've got 80 pages left and 4 days to go. Pages for day 2: 30 You've read 50 out of 100 pages. ** You've got 50 pages left and 3 days to go. Pages for day 3: 0 You've read 50 out of 100 pages. ** You've got 50 pages left and 2 days to go. Pages for day 4: 0 You've read 50 out of 100 pages. ** You've got 50 pages left and 1 days to go. Pages for day 5: 45 You've read 95 out of 100 pages. ** You've got 5 pages left and 0 days to go.
For now, don't worry about the grammar issues ("1 days to go"). And don't worry if the numbers go beyond the total:
$ python reading.py pages to read? 50 days until due? 5 Pages for day 1: 100 You've read 100 out of 50 pages. ** You've got -50 pages left and 4 days to go. Pages for day 2: ... ...
Have your program show the user what they should be reading, based on the pages and days left:
$ python reading.py pages to read? 100 days until due? 4 Pages for day 1: 20 You've read 20 out of 100 pages. ** You've got 80 pages left and 3 days to go. ** You need to read about 26 pages per day! Pages for day 2: 0 You've read 20 out of 100 pages. ** You've got 80 pages left and 2 days to go. ** You need to read about 40 pages per day! Pages for day 3: 45 You've read 65 out of 100 pages. ** You've got 35 pages left and 1 days to go. ** You need to read about 35 pages per day! Pages for day 4: 35 You've read 100 out of 100 pages. ** You've got 0 pages left and 0 days to go.
Remember you may run handin21 as many times as you like. Each time you run it new versions of your files will be submitted. Running handin21 after you finish a program, after any major changes are made, and at the end of the day (before you log out) is a good habit to get into.