Make sure all programs are saved to your cs21/labs/02
directory! Files outside that directory will not be graded.
$ update21
$ cd ~/cs21/labs/02/
$ pwd
/home/username/cs21/labs/02
$ ls
QUESTIONS-02.txt dashes.py reading.py series.py triangle.py
Write a program, dashes.py
, that intersperses dashes between each character in some text entered by the user. In the examples below user input is shown in bold.
$ python3 dashes.py
Enter text: swarthmore
-s-w-a-r-t-h-m-o-r-e-
$ python3 dashes.py
Enter text: CS 21
-C-S- -2-1-
Write a program, triangle.py
, that prints out a triangle made up of asterisks. The user enters an integer to determine how many rows make up the triangle.
$ python3 triangle.py
Number of rows: 3
*
**
***
$ python3 triangle.py
Number of rows: 6
*
**
***
****
*****
******
You can print a blank line with print()
.
Write a program, series.py
, that calculates the sum of the following geometric series:
$$ {1 \over k^0} + {1 \over k^1} + {1 \over k^2} + \ldots + {1 \over k^{n-1}} $$
Your program should ask for the number of terms (n
) and the denominator ratio (k
). For example, if the user enters 2
for k
and 5
for n
, your program should compute the following sum:
$$ 1 + {1 \over 2} + {1 \over 4} + {1 \over 8} + {1 \over 16} $$
$ python3 series.py
denominator ratio (k): 2
number of terms (n): 5
sum: 1.9375
$ python3 series.py
denominator ratio (k): 3
number of terms (n): 7
sum: 1.4993141289437586
$ python3 series.py
denominator ratio (k): 5
number of terms (n): 4
sum: 1.248
There are many practical applications of geometric series. For example, they are used to calculate interest accrued in a bank account over time.
There is a formula for finding the sum of this series, but we expect you to use an accumulator instead of the formula. As an optional, ungraded extra challenge, try to derive the formula. Then compare the answers given by the formula with the answers produced by your program.
Hint for the extra challenge:
Set the sum equal to S:
$$ S = 1 + {1 \over k} + {1 \over k^2} + {1 \over k^3} + {1 \over k^4} + \ldots $$
Then multiply this equation by ${1 \over k}$ to obtain another equation. Using both equations, there is a way to cancel out terms and solve for S.
As both k
and n
become very large, what number does the sum approach? Does this make sense given the formula you derived?
Write a program, reading.py
, that helps a Swarthmore student meet a reading deadline. The program should ask the student how many pages they have to read and how many days they have left to complete the reading. Then it should repeatedly simulate the passing of a day by asking the student how many pages they read, and showing how many pages and days are left.
$ python3 reading.py
Pages to read? 100
Days left? 5
Pages read: 20
You've read 20 out of 100 pages.
80 pages left, 4 days to go.
Pages read: 30
You've read 50 out of 100 pages.
50 pages left, 3 days to go.
Pages read: 0
You've read 50 out of 100 pages.
50 pages left, 2 days to go.
Pages read: 0
You've read 50 out of 100 pages.
50 pages left, 1 days to go.
Pages read: 45
You've read 95 out of 100 pages.
5 pages left, 0 days to go.
If the number of pages read goes beyond the total, the program will show a negative page count. We will soon learn how to handle cases like this more gracefully; for now this is okay.
$ python3 reading.py
Pages to read? 50
Days left? 1
Pages read: 100
You've read 100 out of 50 pages.
-50 pages left, 0 days to go.
Each lab has a short questionnaire at the end. Please edit the QUESTIONS-02.txt
file in your cs21/labs/02
directory and answer the questions in that file.
Don't forget to run handin21
to turn in your lab files! You may run handin21
as many times as you want. Each time it will turn in any new work. We recommend running handin21
after you complete each program or after you complete significant work on any one program.