CS21 Lab 2: numbers and strings...

Due before Midnight Saturday 21 September 2013

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 labs directory, so make sure your programs are in this directory!

1. fun with text

Write a program called textfun.py that prompts the user to enter a phrase and then displays that phrase as a box in the terminal window.

Here are some sample runs of such a program:

$ python textfun.py

Word or phrase: hello

hello
h   h
e   e
l   l
l   l
o   o
hello

$ python textfun.py

Word or phrase: SWARTHMORE

SWARTHMORE
S        S
W        W
A        A
R        R
T        T
H        H
M        M
O        O
R        R
E        E
SWARTHMORE

Note/Hint: since the above boxes are being output to a computer terminal, they must be created one line at a time, from top to bottom. You can't print out the left side of the box, then go back up and print out the right side of the box.

2. Simple Savings Calculator
Write a program called savings.py that helps the user estimate their savings account balance after a year of interest and deposits. At the start of the program ask the user for the following information:

Assuming the bank uses monthly compounding, you can calculate the interest earned in a month as follows:

interest = (current_balance*rate)/12

NOTE: if the rate entered is, say, 6%, in your program you would use 0.06 for the above calculation.

If you know how much interest is earned in a month, you can calculate the new monthly balance by adding the interest and the monthly deposit to the current balance.

Show the user how much interest they earn each month and their current balance each month for one full year. Here are some sample runs of such a program:

 
$ python savings.py

  Initial Deposit: 500
  Monthly Deposit: 20
   Rate of Return: 2

Month:  1    Balance: $520.83   (interest: $0.83)
Month:  2    Balance: $541.70   (interest: $0.87)
Month:  3    Balance: $562.60   (interest: $0.90)
Month:  4    Balance: $583.54   (interest: $0.94)
Month:  5    Balance: $604.51   (interest: $0.97)
Month:  6    Balance: $625.52   (interest: $1.01) 
Month:  7    Balance: $646.56   (interest: $1.04)
Month:  8    Balance: $667.64   (interest: $1.08)
Month:  9    Balance: $688.75   (interest: $1.11)
Month: 10    Balance: $709.90   (interest: $1.15)
Month: 11    Balance: $731.09   (interest: $1.18)
Month: 12    Balance: $752.30   (interest: $1.22)
 
$ python savings.py

  Initial Deposit: 10000
  Monthly Deposit: 100
   Rate of Return: 6.5

Month:  1    Balance: $10154.17   (interest: $54.17)
Month:  2    Balance: $10309.17   (interest: $55.00)
Month:  3    Balance: $10465.01   (interest: $55.84)
Month:  4    Balance: $10621.70   (interest: $56.69)
Month:  5    Balance: $10779.23   (interest: $57.53)
Month:  6    Balance: $10937.62   (interest: $58.39)
Month:  7    Balance: $11096.86   (interest: $59.25)
Month:  8    Balance: $11256.97   (interest: $60.11)
Month:  9    Balance: $11417.95   (interest: $60.98)
Month: 10    Balance: $11579.79   (interest: $61.85)
Month: 11    Balance: $11742.52   (interest: $62.72)
Month: 12    Balance: $11906.12   (interest: $63.61)

NOTE: there are formulas to calculate future balances, but we want you to use an accumulator algorithm for this problem.

Here's an online calculator to test against: bankrate.com

3. sudoku rows and columns
Imagine you are writing a program to display and solve a sudoku puzzle. If the numbers of the puzzle are all stored in one long python list, like this:

puzzle = [9,0,7,5,6,0,0,4,3,0,1,4,0,8,0,0,6,...,8,5,0,0,3,9,6,0,4]

how would you access all numbers for a given row or column? (HINT: use slicing!)

For this problem, assume the puzzle is stored in the python list in row-major order ([all numbers for row 0, then all numbers for row 1, all numbers for row 2, and so on]), and we use 0's to represent empty boxes.

Write a program called sudoku.py that does the following, given the above puzzle as a python list:

Here is a sample run of such a program:
 
$ python sudoku.py

   0  1  2  3  4  5  6  7  8
0 [9, 0, 7, 5, 6, 0, 0, 4, 3]
1 [0, 1, 4, 0, 8, 0, 0, 6, 5]
2 [0, 0, 5, 0, 0, 0, 7, 2, 0]
3 [0, 0, 8, 0, 9, 0, 0, 0, 1]
4 [0, 0, 0, 0, 7, 0, 0, 0, 0]
5 [6, 0, 0, 0, 5, 0, 4, 0, 0]
6 [0, 3, 9, 0, 0, 0, 8, 0, 0]
7 [4, 7, 0, 0, 1, 0, 3, 9, 0]
8 [8, 5, 0, 0, 3, 9, 6, 0, 4]

Row to print: 3
[0, 0, 8, 0, 9, 0, 0, 0, 1]

Column to print: 7
[4, 6, 2, 0, 0, 0, 0, 9, 0]

Number of empty spaces in this puzzle: 46


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