CS21 Lab 2: Numbers and Strings

Due Saturday, September 23, before midnight


Make sure all programs are saved to your cs21/labs/02 directory!


The goals for this lab assignment are:


1. Triangle

Write a program called triangle.py that asks the user for a positive integer. Your program should then create a triangle with this number of rows, like the examples shown below.

$ python3 triangle.py
number of rows: 3
*
**
***

$ python3 triangle.py
number of rows: 10
*
**
***
****
*****
******
*******
********
*********
**********

Extra Challenges (just for fun, no bonus points)

Write a program called triangle_shift.py that also creates a triangle, but now shifted to the right as shown below.

$ python3 triangle_shift.py
number of rows: 3
  *
 **
***

$ python3 triangle_shift.py
number of rows: 10
        *
       **
      ***
     ****
    *****
   ******
  *******
 ********
*********

There are lots of variations of this problem if you would like extra practice. For example, you could create a square or a rectangle based on user input. The user could also choose the symbol to use (or use one symbol for the "outline" and one for the "fill" as shown below).

<<<<<<<<
<^^^^^^<
<^^^^^^<
<^^^^^^<
<^^^^^^<
<^^^^^^<
<^^^^^^<
<<<<<<<<

2. String Reverse

Write a program called reverse.py that asks the user for a text string and then prints out the string reversed, like the example below:

$ python3 reverse.py
text: swarthmore
eromhtraws

Hint: Assume the user types in 'swarthmore'. If we indexed the string in order from 0 to 9, we'd get each character in the string: s, w, a, ... etc. If we could access the the characters is backwards order, by counting from 9 down to 0, we'd get the characters backwards: e, r, o, ... etc. Right now we only know how to count up using range. But notice how we can count backwards even if we have a range that is counting forwards:

>>> for up_index in range(10):
        down_index = 9 - up_index
        print(down_index)
    9
    8
    7
    6
    5
    4
    3
    2
    1
    0

Try to adapt this hint into your solution.


3. Sum of Mathematical Series

Write a program, called series.py, that calculates the sum of the first n terms in the following geometric series:


$$ {1 \over k^0} + {1 \over k^1} + {1 \over k^2} + {1 \over k^3} + {1 \over k^4} + \ldots $$

Your program should ask for the number of terms (n) and the denominator ratio (k), and then calculate the sum including that many terms. For example, if the user enters 2 for k and 5 for n, your program will add up the terms:


$$ 1 + {1 \over 2} + {1 \over 4} + {1 \over 8} + {1 \over 16} $$

There are many practical applications of geometric series, including interest accrued in a bank account over time. When you run your program, it should look like this:

$ python3 series.py
denominator ratio (k): 2
number of terms (n): 5
sum of the first 5 terms is: 1.9375

Here is another example:

$ python3 series.py
denominator ratio (k): 3
number of terms (n): 7
sum of the first 7 terms is: 1.4993141289437586

Here's one more example for testing your program:

$ python3 series.py
denominator ratio (k): 5
number of terms (n): 4
sum of the first 4 terms is: 1.248

Hints:

(Optional) What do you notice about the partial sums above? If you are interested in exploring geometric series further, try to find an analytic formula for the series above with an infinite number of terms. To start, 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. How can you use both equations to cancel out terms and solve for S? This should yield a formula that you can compare with your results above.

As both k and n become very large, what number do your results approach? Does this make sense given your formula above?


4. Petition Signatures

Imagine you are collecting signatures for a petition, and have been asked to write a program to keep track of the current progress.

Write a program that asks the user for the number of signatures needed and the number of days available to obtain them. Your program should then ask for how many signatures were collected each day of the petition, and keep track of both the total obtained and the number remaining. You should also display a 50-character string that shows graphically how far there is to go to reach the petition goal.

After the given number of days, your program should then print out the total number of signatures obtained. It should also print out this number as a percentage of the goal (converted to an int).

Here's a quick example:

$ python3 petition.py
target number of signatures? 100
how many days? 5

day 1
number of petitions: 20

Total signatures so far: 20
Still 80 from target...
##########----------------------------------------

day 2
number of petitions: 50

Total signatures so far: 70
Still 30 from target...
###################################---------------

day 3
number of petitions: 0

Total signatures so far: 70
Still 30 from target...
###################################---------------

day 4
number of petitions: 20

Total signatures so far: 90
Still 10 from target...
#############################################-----

day 5
number of petitions: 10

Total signatures so far: 100
Still 0 from target...
##################################################

>>> 5 DAY TOTAL: 100 SIGNATURES FOR 100 PERCENT <<<

For now, don't worry about invalid input (negative numbers), or if the numbers go beyond the total:

$ python3 petition.py
target number of signatures? 2000
how many days? 3

day 1
number of petitions: -50

Total signatures so far: -50
Still 2050 from target...
---------------------------------------------------

day 2
number of petitions: 2500

Total signatures so far: 2450
Still -450 from target...
#############################################################

day 3
number of petitions: 0

Total signatures so far: 2450
Still -450 from target...
#############################################################

>>> 3 DAY TOTAL: 2450 SIGNATURES FOR 122 PERCENT <<<

Hints:


5. Answer the Questionnaire

Please edit the QUESTIONS-02.txt file in your cs21/labs/02 directory and answer the questions in that file.


Turning in your labs....

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.