CS21 Lab2: Numbers and Strings

Due 11:59pm Tuesday night, September 13

Run update21, if you haven't already, to create the cs21/labs/02. Then cd into your cs21/labs/02 directory and create the python programs for lab 2 in this directory (handin21 looks for your lab 2 assignments in your cs21/labs/02 directory):


$ update21
$ cd cs21/labs/02
$ pwd
  /home/your_user_name/cs21/labs/02

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

1. Computing distance and slope

Write a program, in a file named line.py, that asks the user to enter the value of two points in the Cartesian plane and then computes the slope of the line defined by those points and the distance between the points (formulas are on p.73, exercises 6 and 7).

Here is what two runs of your program might look like:


$ python line.py

Given two points, (x1, y1) and (x2, y2), this program computes
the distance between them and the slope of the line they define

Enter the x coordinate of the first point: 1
Enter the y coordinate of the first point: 1 
Enter the x coordinate of the second point: 4
Enter the y coordinate of the second point: 5

The distance between (1,1) and (4,5) is 5.00
The slope of the line they define is 1.33

$ python line.py
Given two points, (x1, y1) and (x2, y2), this program computes
the distance between them and the slope of the line they define

Enter the x coordinate of the first point: 6
Enter the y coordinate of the first point: 12
Enter the x coordinate of the second point: 10
Enter the y coordinate of the second point: 30

The distance between (6,12) and (10,30) is 18.44
The slope of the line they define is 4.50
To use math library function, remember to add this to the top of your program:
from math import *
You could also try using print string formating to limit the number of places printed beyond the decimal point, but it is not necessary to do so.





2. US Population estimation

Assuming that the current US population is 312 million, how much will it change in the future?

The US Census provides approximate rates of change:

Based on these numbers, write a program (in a file called pop.py) that prompts the user to enter a number of years, N. Your program should then output the current year's population and the approximate population for each of the next N years. Use tabular output (remember that "\t" is the tab character), and include some introductory text that explains what the program is doing, and it should clearly present the results. You may assume that every year is exactly 365 days (you do not have to account for leap year).

Hints:

As an example, here is a run of my program when the user enters 2:

$  python pop.py

This program displays the US population growth chart for the future
Please enter the number of years into the future to display: 2

Year            US Population
------------------------------
2011            312000000
2012            314980325.275
2013            317960650.549
Note: because of floating point round-off error, depending on how you structure your computation, your exact values could differ very slightly from mine beyond the decimal point, but it should be very slight, if at all.



3. Fun with loops and strings...
Write a program, in the file called funstr.py, that prompts the user to enter a string, and then outputs a pyramid of substrings like the following based on the string the user enters:
$ python funstr.py
Please Enter a string: abcdefg
a
ab
abc
abcd
abcde
abcdef
abcdefg

$ python funstr.py
Please Enter a string: Hello-There
H
He
Hel
Hell
Hello
Hello-
Hello-T
Hello-Th
Hello-The
Hello-Ther
Hello-There

$ python funstr.py
Please Enter a string: CS is awesome!
C
CS
CS 
CS i
CS is
CS is 
CS is a
CS is aw
CS is awe
CS is awes
CS is aweso
CS is awesom
CS is awesome
CS is awesome!
Remember space is a character, you just can't always see it.

Hints:



Extra Challenge Problem
This is not a required part of the lab 2 assignment, and you should not try it before completing the required parts. However, if you want an extra challange after completing the required parts of lab2, try the following problem:

Copy your funstr.py file to a new file named funstrarrow.py:

% cp funstr.py funstrarrow.py
In funstrarrow.py, add code to ouput the following pattern shown by the following example (like funstr.py, your solution should work for any string value entered by the user):
$ python funstrarrow.py
Please Enter a string: hello there
h
he
hel
hell
hello
hello 
hello t
hello th
hello the
hello ther
hello there
hello ther
hello the
hello th
hello t
hello 
hello
hell
hel
he
h
note how many times the full string "hello there" appears in the output.
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.