CS 21: Algorithmic Problem Solving

 

Homework #2: Writing Programs

Due 11:59pm Tuesday, February 6

This homework will require you to write some basic programs in python. You should save your programs in your cs21/homework/2 directory. A skeleton version of each program you will write will appear in your cs21/homework/2 if you run update21. The program handin21 will only submit files in this directory.

Your programs are graded on both correctness and style. Please review the comments regarding programming style on the main page

Some of the problems will have optional components that allow you to further practice your skills in python. Optional portions will not be graded, but may be interesting for those wanting some extra challenges.

Atomic weight

[From Zelle, Chapter 3, Problem 3] Write a program by editing the file atomic.py that determines the total molecular weight of a molecule based on the number of hydrogen, carbon, and oxygen atoms it contains. You should use the following weights.

AtomWeight
(g/mol)
H1.0079
C12.011
O15.9994

You program should prompt the user for the number of atoms of each type and display the total weight with the appropriate units. Your output should be formatted to four decimal places. Save your file as atomic.py in your cs21/homework/2 directory.

A sample run is shown below:

   Enter number of Hydrogen atoms: 3
   Enter number of Carbon atoms: 2
   Enter number of Oxygen atoms: 1

   Weight of molecule is 43.0451 g/mol
   
Computing investment value

[From Zelle, Chapter 2, Problem 6] Write a program by editing the file invest.py that computes the value of an interest bearing investment after n years. You should prompt the user for:

Your program should print out a table that contains the following

You may want to look at the futval.py program in section 2.7 of the textbook. You can get a copy of this program by using update21

A sample run is shown below

Enter amount invested at start of each year: 100
Enter the annual interest rate in percent: 10
Enter number of years you plan to invest: 10


 Year      Start   Interest        End      Total
           Value     (year)      Value   Interest
--------------------------------------------------
    1     100.00      10.00     110.00      10.00
    2     210.00      21.00     231.00      31.00
    3     331.00      33.10     364.10      64.10
    4     464.10      46.41     510.51     110.51
    5     610.51      61.05     671.56     171.56
    6     771.56      77.16     848.72     248.72
    7     948.72      94.87    1043.59     343.59
    8    1143.59     114.36    1257.95     457.95
    9    1357.95     135.79    1493.74     593.74
   10    1593.74     159.37    1753.12     753.12
Optional extensions

Fibonacci sequence

[From Zelle, Chapter 3, Problem 16] A Fibonacci sequence is a sequence of numbers where each successive number is the sum of the previous two. The classic Fibonacci sequence begins with
1, 1, 2, 3, 5, 8, 13, ...
Write a program by editing fibo.py that computes the nth Fibonacci number where n is input by the user. For example for n=6 the output should be 8.

A few sample runs are shown below.


Which Fibonacci number would you like?: 1
Fib 1 = 1

Which Fibonacci number would you like?: 2
Fib 2 = 1

Which Fibonacci number would you like?: 7
Fib 7 = 13

Which Fibonacci number would you like?: 8
Fib 8 = 21

Which Fibonacci number would you like?: 9
Fib 9 = 34
  
Optional extensions

Optional Components
As noted above, these questions are NOT required to receive full credit.

Compound interest

Modify invest.py to allow for compounding interest that is compounded m times per year. Ask the user to input the number of times interest is compounded and modify your table to print a line each time interest is computed.

An example is shown below for interest compounded semi-annually:

Enter amount invested at start of each year: 100
Enter the annual interest rate in percent: 10
Enter number of years you plan to invest: 5
Enter the number of times interest is computed each year: 2

   Year      Start   Interest        End      Total
             Value     (term)      Value   Interest
--------------------------------------------------
   1.00     100.00       5.00     105.00       5.00
   1.50     105.00       5.25     110.25      10.25
   2.00     210.25      10.51     220.76      20.76
   2.50     220.76      11.04     231.80      31.80
   3.00     331.80      16.59     348.39      48.39
   3.50     348.39      17.42     365.81      65.81
   4.00     465.81      23.29     489.10      89.10
   4.50     489.10      24.46     513.56     113.56
   5.00     613.56      30.68     644.23     144.23
   5.50     644.23      32.21     676.45     176.45
Compare this to the other table above. What happens as you compound the interest more frequently (quarterly, monthly, daily, hourly). For these tests, you may just want to print out one line for each year, even though you are computing interest many times.

Large Fibonacci numbers

Python can compute using integers of arbitrary length. For example, use your Fibonacci program to compute the 2000th Fibonacci number. Modify you program to print the number of digits in this number. Can you find the first Fibonacci number with over 1000 decimal digits? You may need to try some trial and error to find this number. Later when we discuss conditional loops, we will see how to write a program that will find the answer automatically.

Decimal to binary conversion

Write a program that converts decimal integers to binary. Prompt the user for a decimal number then convert that number to a binary string. Can you come up with a solution that works for all decimal numbers greater than zero? Can you get a solution with no leading zeroes (e.g., 0000101) and/or a solution that also works for zero? Your solution should only use for loops and some math. You may need to import some functions from the math libary such as ceil and log. Remember you can use import math and help(math) to learn how to use these functions.