CS21 Lab 2: Numbers and Strings

Due Saturday (Feb 4) before midnight

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_username]/cs21/labs/02
We will only grade files submitted by handin21 in this directory, so make sure your programs are in this directory!

Goals

The goals for this lab assignment are:



Programming tips/requirements


1. Double Letters

Write a program called double.py that asks the user for a text string. It then creates a new string consisting of each letter doubled from the input string, and displays the new string.

$ python double.py
text: swarthmore college

sswwaarrtthhmmoorree  ccoolllleeggee


2. Sum of Mathematical Series

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

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

Your program should ask for the number of terms, 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 (n): 4

sum of the first 4 terms is: 0.9375

And if the user enters 2, it only sums the first two terms:

 
$ python series.py

number of terms (n): 2

sum of the first 2 terms is: 0.7500
Hints:

Here's one more example for testing your program:

 
$ python series.py

number of terms (n): 10

sum of the first 10 terms is: 0.9990


3. Spell Checker (start of ...)

Given a misspelled word, spell-checking programs work by generating a list of possible alternatives, and then checking each alternative to see if it's a valid word. We can't do the second part of that yet, but one method of generating possible alternative words is by substituting letters for each letter from the original word, one at a time.

For example, if you meant to type "computer", but hit the 'i' key instead of the 'o', the original (misspelled) word would be "cimputer". Clearly, substituting an 'o' for the 'i' gives us a valid word. However, in a real spell checker, we might have to try all letters (a-z) and all substitutions in the original word (i.e., substitute an 'a' for the first letter, substitute an 'a' for the second letter, and so on).

For this program, let's just substitute an 'a' for all letters in the given word. Here's an example:

$ python substitution.py
word: zebrq

 0: aebrq
 1: zabrq
 2: zearq
 3: zebaq
 4: zebra

In the above example, I accidentally hit 'q' when I meant to hit 'a'. The program substitutes an 'a' for all letters in the original word. If we could look up all of the above words in an English dictionary, at least one of them would be a valid word!

Write a program called substitution.py that prompts the user to enter a word and then displays all possible substitutions of the letter 'a' for that word.

Here is another sample run of the program:

$ python substitution.py

word: drzgon

 0: arzgon
 1: dazgon
 2: dragon
 3: drzaon
 4: drzgan
 5: drzgoa

Hint: use string slicing in this program! For example, if you are trying to substitute the 5th letter of the word, use slicing to get all letters before the 5th letter and all letters after the 5th letter.

Extra Challenge (just for fun, no bonus points)

For the above program, display all substitutions, using all letters from 'a' to 'z'.

 
$ python substitution.py

word: xebra

 0: aebra
 1: xabra
 2: xeara
 3: xebaa
 4: xebra
 0: bebra
 1: xbbra
 2: xebra
 3: xebba
...
...
 0: yebra
 1: xybra
 2: xeyra
 3: xebya
 4: xebry
 0: zebra
 1: xzbra
 2: xezra
 3: xebza
 4: xebrz

4. Fundraiser

Imagine you are part of a fundraiser, and have been asked to write a program to keep track of the money raised.

Write a program that asks the user for the amount of money to raise and the number of days to raise it. Your program should then ask for how much was raised during each day of the fundraiser, and keep track of both the total raised and the amount left to raise. You should also display a 50-character string that shows graphically how far there is to go to reach the fundraising goal.

Here's a quick example:

 
$ python fundraiser.py
fundraising goal? $100
   how many days? 5

day 1 amount: $20

Total raised so far: $20
Still $80 from target...
$$$$$$$$$$----------------------------------------

day 2 amount: $50

Total raised so far: $70
Still $30 from target...
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$---------------

day 3 amount: $0

Total raised so far: $70
Still $30 from target...
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$---------------

day 4 amount: $20

Total raised so far: $90
Still $10 from target...
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$-----

day 5 amount: $10

Total raised so far: $100
Still $0 from target...
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

>>> TOTAL RAISED IN 5 DAYS $100 (100%) <<<

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

 
$ python fundraiser.py
fundraising goal? $2000
   how many days? 3

day 1 amount: $-50

Total raised so far: $-50
Still $2050 from target...
---------------------------------------------------

day 2 amount: $2500

Total raised so far: $2450
Still $-450 from target...
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

day 3 amount: $0

Total raised so far: $2450
Still $-450 from target...
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

>>> TOTAL RAISED IN 3 DAYS $2450 (123%) <<<
Hints:

5. Answer the Questionnaire; Run handin21

Once you are confident that all three programs work, fill out the questionnaire in README-02.txt. Then run handin21 a final time to make sure we have access to the most recent versions of the files required for this lab.