CS21 Lab 2: Numbers and strings

Due before midnight, Saturday September 20

This lab assignment requires you to write more programs in python. As always, 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/02
We will only grade files submitted by handin21 in this labs directory, so make sure your programs are in this directory!

1. Spell checking...sort 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 to simply delete each letter from the original word, one at a time. For example, if you meant to type "computer", but actually hit the 't' key twice, here are all possible "deletions" of the original word:

omputter  (the first letter deleted)
cmputter  (the second letter deleted)
coputter  (and so on)
comutter
comptter
computer  <-- a valid word!
computer
computtr
computte

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 spellchecker.py that prompts the user to enter a word and then displays all possible deletions of that word.

Here is a sample run of such a program:

$ python spellchecker.py

word: oranmge

DELETIONS:
0 ranmge
1 oanmge
2 ornmge
3 oramge
4 orange
5 oranme
6 oranmg 

Another way to generate possible alternative words is by transposing every pair of letters, one at a time. For example, sometimes I type "teh" when I mean to type "the". Transposing the "e" and the "h" would catch this misspelling.

Add transpositions to your spellchecker.py code.


$ python spellchecker.py

word: abcd

DELETIONS:
0 bcd
1 acd
2 abd
3 abc

TRANSPOSITIONS:
0 bacd
1 acbd
2 abdc 

2. 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 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.

$ python fundraiser.py

What is the fundraising goal? $10000
and how many days do we have to raise it? 5

How much was raised today (day  1)? 1000
Total raised so far: $1000
We are still $9000 from the target.
$$$$$---------------------------------------------GOAL = $10000

How much was raised today (day  2)? 4000
Total raised so far: $5000
We are still $5000 from the target.
$$$$$$$$$$$$$$$$$$$$$$$$$-------------------------GOAL = $10000

How much was raised today (day  3)? 2000
Total raised so far: $7000
We are still $3000 from the target.
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$---------------GOAL = $10000

How much was raised today (day  4)? 1000
Total raised so far: $8000
We are still $2000 from the target.
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$----------GOAL = $10000

How much was raised today (day  5)? 400
Total raised so far: $8400
We are still $1600 from the target.
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$--------GOAL = $10000

Total raised $8400

NOTE: you do not have to worry about going over the fundraising goal.

HINT: You can use string formatting anywhere you use a string in python (not just in a print statement). For example:

>>> day = "Monday"
>>> n = input("How many books did you read on %s? " % (day))
How many books did you read on Monday? 3
>>> n
3

3. Three-character window

Write a program that, given a string of characters, displays all three-character sequences along the string. For example, given the string "ABCDEFG", your program would print out "ABC", then "BCD", then "CDE", and so on.

Have your program format the output such that the three characters to print are shown, but the rest of the string is displayed as pound signs (like a three-character window moving down the string).

Here is a sample run of such a program:

 
$ python charwindow.py

text: ABCDEFGHIJKLMNOP

ABC#############
#BCD############
##CDE###########
###DEF##########
####EFG#########
#####FGH########
######GHI#######
#######HIJ######
########IJK#####
#########JKL####
##########KLM###
###########LMN##
############MNO#
#############NOP

Extra Challenge (just for fun...no bonus points): ask the user for the size of the character window.

Submit

Once you are satisfied with your programs, hand them in by typing handin21 at the unix prompt.

You may run handin21 as many times as you like. We will grade the most recent submission submitted prior to the deadline

Remember: for this lab, programs must appear in your cs21/labs/02 directory. If you create your programs in a different directory, use the unix mv or cp commands to move or copy them into the cs21/labs/02 directory. For example:

 cp  myprog.py  ~/cs21/labs/02/myprog.py