CS21 Lab2: Numbers and Strings

Due 11:59pm Tuesday night, September 16

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
Try using string formating to limit the number of places printed beyond the decimal point.

To use math library function, remember to add this to the top of your program:

from math import *


2. Zeno's Dichotomy Paradox
Zeno, a 5th century BC Greek philosopher wrote "40 `paradoxes of plurality', attempting to show that ontological pluralism - a belief in the existence of many things rather than only one - leads to absurd conclusions" [from the Stanford Encyclopedia of Philosophy]

Zeno's Dichotomy Paradox is a paradox about continuous motion: before one can reach his/her destination he/she must first arrive at the half-way point, but before he/she can reach the half-way point, he/she must first arrive at the half-way point of the half-way point, and so on. Since this process continues forever, one can never reach his/her destination.

Zeno's paradox can be written mathematically as the sum of the following infinite series:


   1      1      1      1     1        
  --  +  --  +  --  +  -- +  --  + ...
   2      4      8     16    32
Since today we know that infinite series can have finite sums, we may not find Zeno's paradox so paradoxical.

Write a program, in a file named zeno.py, that computes the sum of the first n terms in the above series, given a value for n that is entered by the user. For example, a couple runs of your program may look like this:


$ python zeno.py
This program computes the sum of the first n terms
of the series based on Zeno's Dichotomy paradox
Enter a value a value for n: 3 

the sum of the first 3 terms is  0.875


$ python zeno.py
This program computes the sum of the first n terms
of the series based on Zeno's Dichotomy paradox
Enter a value a value for n: 20

the sum of the first 20 terms is  0.999999046326


3. Acronyms
Write a program in a file named acronyms.py, that prompts the user to enter a phrase, and then converts the phrase to it acronym. Here are a couple runs of what your program might look like:

$ python acronyms.py
This program creates acronyms from a phrase.
Enter a phrase: the World needs one more acronym
The acronym of "the World needs one more acronym" is: TWNOMA


$ python acronyms.py
This program creates acronyms from a phrase.
Enter a phrase: to be or not to be
The acronym of "to be or not to be" is: TBONTB


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.