CS21 Lab2: Numbers and Strings

Due Saturday (Sept 12) before midnight

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

Programming tips

As you write your first programs, start using good programming practices now:


1. Patterns

(image credit)
(Special thanks to Tom Wexler at Oberlin College for the pattern examples provided here.)

In this section, you will actually write three small programs to solve three small problems. For each problem, a few patterns are given. Each pattern is comprised solely of numbers and spaces. The pattern that gets printed depends on the number that the user enters. Your job is to first determine the pattern that dictates how a particular figure in the sequence relates to the corresponding number, then write the program that generates the pattern shown.

Pattern A (patternA.py)

For the first pattern, pattern A, write a program called patternA.py. You should read an integer from the user and then print out the pattern.

$ python patternA.py
Enter a number to generate a cool pattern: 3
1 2 3
1 2 3
1 2 3

$ python patternA.py
Enter a number to generate a cool pattern: 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

$ python patternA.py
Enter a number to generate a cool pattern: 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

You should be able to deduce the pattern from the examples above. Your program will first get an integer from the user and then print the appropriate pattern.

Pattern B (patternB.py)

For the second pattern, pattern B, write a program called patternB.py. You should read an integer from the user and then print out the pattern.

$ python patternB.py
Enter a number to generate a cool pattern: 3
1 1 1
2 2 2
3 3 3

$ python patternB.py
Enter a number to generate a cool pattern: 4
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4 

$ python patternB.py
Enter a number to generate a cool pattern: 5
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5

Pattern C (patternC.py)

For the last pattern, pattern C, write a program called patternC.py. You should read an integer from the user and then print out the pattern.

$ python patternC.py
Enter a number to generate a cool pattern: 3
1 2 3
2 3
3

$ python patternC.py
Enter a number to generate a cool pattern: 4
1 2 3 4
2 3 4
3 4
4

$ python patternC.py
Enter a number to generate a cool pattern: 5
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
2. Missing letters

(photo credit)

Write a program that, given a string of characters, displays the phrase with all 3-character sequences along the string missing. Have your program format the output such that the 3-characters that are omitted are replaced by asterisks.

Here is a sample run of such a program:

 
$ python missing.py

text: ABCDEFGHIJKLMNOP

***DEFGHIJKLMNOP
A***EFGHIJKLMNOP
AB***FGHIJKLMNOP
ABC***GHIJKLMNOP
ABCD***HIJKLMNOP
ABCDE***IJKLMNOP
ABCDEF***JKLMNOP
ABCDEFG***KLMNOP
ABCDEFGH***LMNOP
ABCDEFGHI***MNOP
ABCDEFGHIJ***NOP
ABCDEFGHIJK***OP
ABCDEFGHIJKL***P
ABCDEFGHIJKLM***
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.)

Extra Challenge (just for fun): ask the user for the number of letters to hide from each line instead of always doing 3.

3. CS Department Co-op

(photo credit)

Write a program that runs the checkout at the CS department Co-op technology store. Your program should first prompt the user for the number of items in their shopping cart. For example, if the user purchased a usb key, a keyboard, and headphones, the user should enter 3. Next, prompt the user for the name of the item they purchased and the cost of that item. Your program should then compute the total cost of the purchase and print a receipt. Your program should use print formatting to show the costs with two decimal places. Here are some sample runs showing how your program should work:

$ python store.py 

Welcome to the Swarthmore CS co-op

Enter number of unique items: 3

Enter the item purchased: usb key
Enter the cost: 9.25

Enter the item purchased: keyboard
Enter the cost: 23.00

Enter the item purchased: beats headphones
Enter the cost: 199.99

Thank you for shopping at the CS co-op.
Here is your receipt:

             usb key $   9.25
            keyboard $  23.00
    beats headphones $ 199.99

               TOTAL $ 232.24
$ python store.py 

Welcome to the Swarthmore CS co-op

Enter number of unique items: 1

Enter the item purchased: macbook
Enter the cost: 1399.99

Thank you for shopping at the CS co-op.
Here is your receipt:

             macbook $1399.99

               TOTAL $1399.99

Submit

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.