CS21 Lab 1: First programs

Due by 11:59pm Friday, Feb 1, 2013

This lab assignment requires you to write three programs in python. First, run update21. This will create the cs21/labs/01 directory (if you haven't already) and copy over any starting-point files for your programs. Next, move into your cs21/labs/01 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/01
$ pwd
/home/your_user_name/cs21/labs/01
The program handin21 will only submit files in this cs21/labs/01 directory, so make sure your programs are in this directory!

1. Running laps

Running 1 mile is about 1600 meters, which is 4 laps on the Swarthmore track (each lap is 400m). Write a program that asks the user for the number of miles they want to run and calculates the number of laps they have to do.

Here are some sample runs of such a program:

$ python laps.py

Miles you want to run: 6
That's  24  laps on the track.

$ python laps.py

Miles you want to run: 0.5
That's  2.0  laps on the track.

$ python laps.py

Miles you want to run: 26
That's  104  laps on the track.


2. Rainfall

When the weather forecast calls for one inch of rain, how many gallons of water fall on Swarthmore College?

Assuming the college is about 300 acres, and looking up some conversion factors, we can write this mathematically as:

gallons of rain = (# of inches/12 inches per foot) * 300 acres * 43560 sq ft per acre * 7.48052 gallons per cubic foot

Write a program that asks the user for an amount of rain (in inches) and then calculates and displays the approximate number of gallons that fall on Swarthmore College.

Here are some sample runs of such a program:

 
$ python rain.py

Rainfall in inches: 1
That's about 8146286.28 gallons of rain on Swarthmore College.

$ python rain.py

Rainfall in inches: 0
That's about 0.0 gallons of rain on Swarthmore College.

$ python rain.py

Rainfall in inches: 2.5
That's about 20365715.7 gallons of rain on Swarthmore College.


3. Base-2 logarithms

Base-2 logarithms are very useful in computer science. In python we can easily calculate them using the math library, like this:

>>> from math import *
>>> log(8,2)
3.0
(log(8,2) is log-base2 of 8.0).

Write a program to calculate and display a table of base-2 logarithms from 1 to some number the user enters, like this:

$ python logs.py

End value: 20

log-base2 of  1  is  0.0
log-base2 of  2  is  1.0
log-base2 of  3  is  1.58496250072
log-base2 of  4  is  2.0
log-base2 of  5  is  2.32192809489
log-base2 of  6  is  2.58496250072
log-base2 of  7  is  2.80735492206
log-base2 of  8  is  3.0
log-base2 of  9  is  3.16992500144
log-base2 of  10  is  3.32192809489
log-base2 of  11  is  3.45943161864
log-base2 of  12  is  3.58496250072
log-base2 of  13  is  3.70043971814
log-base2 of  14  is  3.80735492206
log-base2 of  15  is  3.90689059561
log-base2 of  16  is  4.0
log-base2 of  17  is  4.08746284125
log-base2 of  18  is  4.16992500144
log-base2 of  19  is  4.24792751344
log-base2 of  20  is  4.32192809489

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, and only the most recent submission will be recorded. This is useful if you realize, after handing in some programs, that you'd like to make a few more changes to them.

Remember: for this lab, handin21 will only submit files from your cs21/labs/01 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/01 directory. For example:

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