General Notes

Problem Set 1

Submit the following programs via Blackboard:

  1. Due Date: 15 September Reading: Chapters 1 & 2
    Write a program that asks the user for a name and then prints your input to the screen.
  2. Due Date: 15 September Reading: Chapters 1 & 2
    Write a program that asks the user for two integers and then prints the sum and the product of the two given integers.
  3. Due Date: 15 September Reading: Chapters 1 & 2
    Write a program that asks the user for three integers and then prints the product of the three integers as well as the square root of the product.
  4. Due Date: 15 September Reading: Chapters 1 & 2
    Write a program that converts weights measured in kilograms to pounds. One kilogram is 2.2 pounds. (Hint: see the convert.py program from the book's webpage.)
  5. Due Date: 15 September Reading: Chapters 1 & 2
    Write a program that will convert from dollars to another currency. The currency you should use is based on the first letter of your first name:

    For example, if your first name is Eric, you would use Euros.

    If your name begins with Use the currency $1 is worth
    A Afghan Afghani (AFN) 51.08
    B Bangladeshi Taka (BDT) 80.58
    C Costa Rican Colon (CRC) 499.38
    D Danish Krone (DKK) 5.67
    E Euro (EUR) 0.77
    F Falkland Island Pound (FKP) 0.62
    G Guatemalan Quetzal (GTQ) 0.13
    H Hungarian Forint (HUF) 0.00456
    I Indonesian Rupiah (IDR) 9639.99
    J Japanese Yen (JPY) 83.85
    K Kenyan Shilling (KES) 0.0116
    L Lebanese Pound (LPP) 0.000664
    M Moroccan Dirham (MAD) 0.118
    N Nepalese Rupee (NPR) 0.0115
    O Omani Rial (OMR) 0.384
    P Polish Zloty 3.11
    Q Qatari Riyal (QAR) 0.275
    R Russian Ruble (RUB) 30.80
    S Somali Shilling (SOS) 0.000619
    T Thai Baht (THB) 30.59
    U Ukrainian Hryvna (UAH) 8.10
    V Venezuelan Bolivar (VEH) 4.30
    W Samoan Tala (WST) 2.27
    X East Caribbean Dollar (XCD) 2.70
    Y Yemeni Rial (YER) 0.00477
    Z Zimbabwean Dollar (ZWD) 361.90

Problem Set 2

Submit the following programs via Blackboard:

  1. Due Date: 22 September
    Write a program that asks the user for distance in feet and returns the number of miles and feet (use the conversion rate of 5280 feet per mile). For example, if the user entered, 10000, your program would output, 1 mile 4720 feet.
    Note that you should use "miles" and "feet" for all inputs (even in the cases where it is grammatically incorrect).
  2. Due Date: 22 September
    Write a program that takes as two inputs from the user: the zone and the ticket type, and returns the Copenhagen Transit fare.
    • If the zone is 2 or smaller and the ticket type is "adult," the fare is 23.
    • If the zone is 2 or smaller and the ticket type is "child," the fare is 11.5.
    • If the zone is 3 and the ticket type is "adult," the fare is 34.5.
    • If the zone is 3 or 4 and the ticket type is "child," the fare is 23.
    • If the zone is 4 and the ticket type is "adult," the fare is 46.
    • If the zone is greater than 4, the fare is -1.00 (since your calculator does not handle inputs that high).
  3. Due Date: 22 September
    Write a program that allows the user to choose which currency they would like to convert. Using the chart from Problem 1-5, choose the currencies that begin with the first five unique letters of your name. You should print out the currencies your program will convert, ask the user for their currency choice, and then the amount of money to be converted. Your program should then print the amount of money in the chosen currency. For example:
    Sameh's Currency Converter
    Converts from dollars into Somali Shilling, Afghan Afghanis, Moroccan Dirhams, 
    Euros, and Hungarian Forints.
    Choose a currency ('S', 'A', 'M', 'E', 'H'):  h
    What amount: 1000
    
    The chosen currency is:  Hungarian Forints
    The converted amount is:  4.56 
    	 
  4. Due Date: 22 September
    Write a program that asks a user to enter a list of 4 clothing items purchased. Your program should then calculate their total bill with tax. In New York City, the tax on clothing items that priced $110 or less is 0%. The tax on clothing items priced more than $110 is 8.5%.

    Here is a sample run of the program:

    Please enter clothing prices (4 items), separated by commas:  100, 9.99, 200, 159.99	
    		
    Your receipt:
    	    100.00
    	      9.99
    	    200.00  T
    	    159.99  T
    	 ---------
    	    500.58 
             

    (The `T' indicates which items are taxed. Do not worry about alligning the output in the receipt, we will learn how to do that at a later time.

  5. Due Date: 22 September
    Write a program to calculate how much money you would have if you spend half of your money every day. Your program should ask the user for the starting amount of money and then display the amount of money for the next 6 days. So, if the starting amount is $1000000. Here is a sample interaction:
    Please enter the starting amount of money:  1000000
    Day 1:  $1000000
    Day 2:  $500000
    Day 3:  $250000
    Day 4:  $125000
    Day 5:  $62500
    Day 6:  $31250
    	 

Problem Set 3

Submit the following programs via Blackboard:

  1. Due Date: 29 September
    Write a program that takes input from the user specifying the zone and the direction of travel, "inbound" or "outbound", on the San Fransicso Bay Area Rapid Transit (BART). Your program will use this information to compute the fare as follows:
    • If the zone is 2 or smaller, the fare is 1.75.
    • If the zone is 3 and the direction is outbound, the fare is 2.15.
    • If the zone is 3 and the direction is inbound, the fare is 2.45.
    • If the zone is 4 and the direction is outbound, the fare is 2.65.
    • If the zone is 4 and the direction is inbound, the fare is 2.95.
    • If the zone is greater than 4, the fare is -1.00 (since your calculator does not handle inputs that high).
  2. Due Date: 29 September
    Write a program that asks the user for an integer input between 0 and 9. If the input is valid (0 <= input <= 9), your program should print out the corresponding string. For example, if the input is 1, your program should print out one. If the input is 2, your program should print out two, etc. If the input is not valid, your program should print an error message. Example:
    	   >>> 
    	   Enter an integer between 0 and 9 (inclusive): 9
    	   nine
    
    	   >>> 
    	   Enter an integer between 0 and 9 (inclusive): 10
    	   Error: invalid input.	   
    	 
  3. Due Date: 29 September
    Write a program that asks the user for an integer input between 1 and 7. If the input is valid (1 <= input <= 7), your program should print the corresponding day as a string. For example, if the input is 1, your program should print out Monday. If the input is 2, your program should print out Tuesday, etc. If the input is not valid, your program should print out an error message. Example:
    	   >>> 
    	   Enter an integer between 1 and 7 (inclusive): 1
    	   Monday
    	   
    	   >>> 
    	   Enter an integer between 1 and 7 (inclusive): 2
    	   Tuesday
    	   
    	   >>> 
    	   Enter an integer between 1 and 7 (inclusive): 8
    	   Error: invalid input.
    	 
  4. Due Date: 29 September
    The algorithm below is written in pseudo-code (an informal, high-level description of what a program should do). Convert the pseudocode into a python program:
    Ask user for an integer
    if the number is even
       print a message saying that even numbers are great
    else
       print a message saying what an odd number 
    	 

    Hint: An easy way to test if a number is even is to see if there is a remainder after dividing by 2. For example 5 divided by 2 has remainder 1 so is not even. While 12 divided by 2 has remainder 0 so is even.

  5. Due Date: 29 September
    Write a program that asks the user for a number between 0 and 10 (inclusive). If the user enters a number that is larger than 10, print out a message, saying: "too big!". If the user enters a number that is smaller than 0, print out a message, saying: "too small". Otherwise, print out the message: "just right!"

Problem Set 4

Submit the following programs via Blackboard:

  1. Due Date: 6 October
    Write a program that prompts the user for their birth year. The valid range of years should be a number between 1940 and 1999, inclusive. If they enter a number out of range, print a message that the number is out of range and prompt them again with the same prompt. When the user enters a number in the correct range, compute the user's age and print the age to the screen and end the program. Example:
    	   Enter the year you were born (between 1940 and 1999 inclusive): 2001
    	   Invalid input, please try again...
    	   Enter the year you were born (between 1940 and 1999 inclusive): 1930
    	   Invalid input, please try again...
    	   Enter the year you were born (between 1940 and 1999 inclusive): 1982
    	   This year you turn 31
    	 
  2. Due Date: 6 October
    Write a program that prompts the user for a positive number of lines and then prints out your name that many times to the screen. If they enter a non-positive number, print a message that the number is invalid and prompt them again with the same prompt. Example: if your name is Sandeep:
    	   How many times should I print my name? (Enter a positive number): -17
    	   Invalid input, please try again...
    	   How many times should I print my name? (Enter a positive number): 0
    	   Invalid input, please try again...
    	   How many times should I print my name? (Enter a positive number): 4
    	   Sandeep
    	   Sandeep
    	   Sandeep
    	   Sandeep
    	 
  3. DueDate: 6 October
    Write a program that prompts the user for a number, in between 1 and 50, inclusive. If they enter a number out of range, print a message that the number is out of range and prompt them again with the same prompt. When the user enters a number in the correct range, your program then prints out a square grid of stars using that number as the dimension. For example:
    	   How big a grid would you like? (Between 1 and 50 inclusive): 67
    	   Invalid input, please try again...
    	   How big a grid would you like? (Between 1 and 50 inclusive): -2
    	   Invalid input, please try again...
    	   How big a grid would you like? (Between 1 and 50 inclusive): 5
    
    	   *****
    	   *****
    	   *****
    	   *****
    	   *****
    	 
  4. Due Date: 6 October
    Write a program that prompts the user for a number, in between 1 and 50, inclusive. If they enter a number out of range, print a message that the number is out of range and prompt them again with the same prompt. When the user enters a number in the correct range, your program then prints out a triangle of stars using that number as the dimension. For example:
    	   How big a triangle would you like? (Between 1 and 50 inclusive): 82
    	   Invalid input, please try again...
    	   How big a triangle would you like? (Between 1 and 50 inclusive): -9
    	   Invalid input, please try again...
    	   How big a triangle would you like? (Between 1 and 50 inclusive): 6
    
    	   *
    	   **
    	   ***
    	   ****
    	   *****
    	   ******
    	 
  5. Due Date: 6 October
    Write a program that prompts the user for a positive number and then prints out all multiples of 7 up to that number. If they enter a non-positive number, print a message that the number is invalid and prompt them again with the same prompt. For example:
    	   Please enter a positive number: -5
    	   Invalid input, please try again...
    	   Please enter a positive number: 0
    	   Invalid input, please try again...
    	   Please enter a positive number: 50
    	   The multiple of 7 below 50 are:
    	   7
    	   14
    	   21
    	   28
    	   35
    	   42
    	   49
    	 

Problem Set 5

Submit the following programs via Blackboard:

  1. Due Date: 20 October
    Translate the following pseudocode into a python program:
    	   x = prompt the user for a positive integer and verify input
    	   y = x
    	   while x > 1:
    		print "x="x, "y="y
    		x = x/2
    		y = y*2
    	 
    For example:
    	   Please enter a positive number: -5
    	   Invalid input, please try again...
    	   Please enter a positive number: 0
    	   Invalid input, please try again...
    	   Please enter a positive number: 50
    	   x=50 y=50
    	   x=25 y=100
    	   x=12.5 y=200
    	   x=6.25 y=400
    	   x=3.125 y=800
    	   x=1.5625 y=1600
    	 
  2. Due Date: 20 October
    Translate the following pseudocode into a python program that uses a for loop:
    	   end = prompt the user for a positive integer and verify input
    	   i = 0
    	   while i < end:
    		print "i="i, "squared="i2, "cubed="i3
    		i = i + 1
    	 
    For example:
    	   Please enter a positive number: -5
    	   Invalid input, please try again...
    	   Please enter a positive number: 0
    	   Invalid input, please try again...
    	   Please enter a positive number: 6
    	   i=0 squared=0 cubed=0
    	   i=1 squared=1 cubed=1
    	   i=2 squared=4 cubed=8
    	   i=3 squared=9 cubed=27
    	   i=4 squared=16 cubed=64
    	   i=5 squared=25 cubed=125
    	 
  3. Due Date: 20 October
    Translate the following pseudocode into a python program that uses a for loop:
    	   iterations = prompt the user for a positive integer and verify input
    	   i = 6
    	   end = i + (iterations * 2) - 1
    	   while i < end:
    		print "i="i, "square root="√i
    		i = i + 2
    	 
    For example:
    	   Please enter a positive number: -5
    	   Invalid input, please try again...
    	   Please enter a positive number: 0
    	   Invalid input, please try again...
    	   Please enter a positive number: 5
    	   i=6 square root=2.449489742783178
    	   i=8 square root=2.8284271247461903
    	   i=10 square root=3.1622776601683795
    	   i=12 square root=3.4641016151377544
    	   i=14 square root=3.7416573867739413
    	 
  4. Due Date: 20 October
    Write a program that asks the user for a positive number and verify the input. Then use a for loop to compute the factorial of a number. Your program should print out the original number, and its factorial.

    As a reminder, factorial is a mathematical function that multiplies all the nonnegative integers below n. For instance, to compute 5 factorial:

    5! = 5 × 4 × 3 × 2 × 1 = 120

    In addition, 0! = 1. Your program should prompt the user for a positive number and verify that the result is positive.

    For example:
    	   Please enter a positive number: -5
    	   Invalid input, please try again...
    	   Please enter a positive number: 0
    	   0! = 1
    	 
    Another example:
    	   Please enter a positive number: 4
    	   4! = 24
    	 
  5. Due Date: 20 October
    Redo problem 5-4, this time using a while loop.

Problem Set 6

Submit the following programs via Blackboard:

  1. Due Date: 27 October
    Write a program to print the lyrics of the song "Old MacDonald." Your program should print the lyrics for five different animals, similar to the example verse below.

    Old MacDonald had a farm, Ei-igh, Ee-igh, Oh!
    And on that farm he had a cow, Ee-igh, Ee-igh, Oh!
    With a moo, moo here and a moo, moo there.
    Here a moo, there a moo, everywhere a moo, moo.
    Old MacDonald had a farm, Ei-igh, Ee-igh, Oh!

    (Hint: use a function with two input parameters one for the animal and the other for the related sound)
  2. Due Date: 27 October
    Fill in the missing function definitions for this program:
    	   def main():
    	      x1,y1 = getpoint()	   # prompt user for the coordinates of a point (x1,y1)
    	      x2,y2 = getpoint()	   # prompt user for the coordinates of a point (x2,y2)
    	      d = getDistance(x1,y1,x2,y2) # Calculates and returns distance from (x1,y1) to (x2,y2)
    	      display(d)		   # Displays the calculated distance.
    
    	   main()
    	 

    That is, write the functions getPoint(), getDistance(), and display(). Include all functions, including the main() function above in the file you submit.

    Hint: The distance between two points (x1,y1) and (x2,y2) is given by d = sqrt((x2 - x1)2 + (y2 - y1)2)

  3. Due Date: 27 October
    Write a function, day() that accepts one parameter, a number between 1 and 7 and prints out the corresponding day as a string. If the number is less than 1 or greater than 7, the day() function should print out "No Such Day". For example, if the parameter is 1, your function should print out "Monday". If the parameter is 2, your function should print out "Tuesday", etc. Your program should have a main() function that calls the day() function and exercises all the possibilities.
  4. Due Date: 27 October
    Write a function, digit() that accepts one parameter, a number between 0 and 9 and returns the corresponding digit as a string. If the number is less than 0 or greater than 9, the digit() function should return "No Such Digit". For example, if the parameter is 1, your function should return "One". If the parameter is 2, your function should return "Two", etc. Your program should have a main() function that calls the digit() function exercises all the possibilities and displays the results.
  5. Due Date: 27 October
    Write a function, number() that accepts one parameter, a number between 0 and 99 and returns the corresponding number as a string. If the number is less than 0 or greater than 99, the number() function should return "Number Out Of Range". For example, if the parameter is 52, your function should return the string "Fifty Two" or if the parameter is 75, your function should return the string "Seventy Five". Your program should have a main() function that contains a loop starting at -1 and ending at 100 that calls the number() function and displays the results.

Problem Set 7

Submit the following programs via Blackboard:

  1. Due Date: 3 November
    Write a program that takes ten mouse clicks from the user and draws a polygon whose corners, or vertices, are the clicked points. Hint: As you go, add a line from the last point clicked to the first point clicked.

    Be sure to include code to close the window following a final mouse click. Points will be deducted if the grader's computer crashes. To do this with Zelle's graphics, use:
    	  win.getMouse()
    	  win.close()
    	
  2. Due Date: 3 November
    Using Zelle's graphics module, draw out your initials (the first letter of your first and last names). Your display should contain 5 or more lines, circles, or other graphic objects. So, if your initials are very simple to draw (for example, `LI'), draw additional letters from your name.

    Be sure to include code to close the window following a final mouse click. Points will be deducted if the grader's computer crashes. To do this with Zelle's graphics, use:
    	  win.getMouse()
    	  win.close()
    	
  3. Due Date: 3 November
    Using Zelle's graphics module, write a program that displays a solid colored object (you can pick the object, location, and shape). When the user clicks on the window, change the color of the object. Your program should allow the user to click 5 times before ending the program and closing the window.

    Be sure to include code to close the window following a final mouse click. Points will be deducted if the grader's computer crashes. To do this with Zelle's graphics, use:
    	  win.getMouse()
    	  win.close()
    	
  4. Due Date: 3 November
    Write a graphics program that changes colors depending where the user clicks on the window. If the user clicks on the left side, your name (or initials) should appear in green in the window. If the user clicks on the right side, your name (or initials) should appear in red in the window. (You may write your name in text or initials using graphics or text, and you may color the letters or the background-- just as long as it is clear hat the color has changed when the mouse is clicked on the left or the right). Your program should allow the user to click 5 times before ending the program and closing the window.

    Be sure to include code to close the window following a final mouse click. Points will be deducted if the grader's computer crashes. To do this with Zelle's graphics, use:
    	  win.getMouse()
    	  win.close()
    	
  5. Due Date: 3 November
    Write a program that takes two mouse clicks from the user and draws rectangle whose corners include the two points clicked. Color the lines of the rectangle in red and fill the inside with green.

    Be sure to include code to close the window following a final mouse click. Points will be deducted if the grader's computer crashes. To do this with Zelle's graphics, use:
    	  win.getMouse()
    	  win.close()
    	

Problem Set 8

Submit the following programs via Blackboard:

  1. Due Date: 10 November
    Write a program that uses turtle graphics to rotate your turtle. In your program, create a turtle named for you (for example, if your name is Tom, the name of your turtle variable should be tom). Ask the user to enter a number, n. If that number is even, your turtle should turn n degrees to the right. If that number is odd your turtle should turn n degrees to the left. After rotating, you should have your turtle walk forward 10 steps. For example, if n is even, you would have:
    	   tom.right(n)
    	   tom.forward(10)
    	 
    If n is odd, you would have:
    	   tom.left(n)
    	   tom.forward(10)
    	 


    Be sure to include code to close the window following a final mouse click. Points will be deducted if the grader's computer crashes. To do this with turtle graphics, use:
    	   scr.exitonclick()
    	 
  2. Due Date: 10 November
    Translate the following pseudocode into a python function that uses turtle graphics:
    	   hexagon(turtle, x):
    	      while x > 10:
    	         for i = 0 to 6:
    		    move turtle forward x steps
    		    turn turtle 60 degrees to right
    		 x = x -10
    	 
    Include in your submitted program a main function that creates a turtle and calls the the hexagon function with your turtle and 50 as the input parameters.

    Be sure to include code to close the window following a final mouse click. Points will be deducted if the grader's computer crashes. To do this with turtle graphics, use:
    	   scr.exitonclick()
    	 
  3. Due Date: 10 November
    Use turtle graphics to simulate a random walk of 1000 steps. In your program, create a turtle named for you (for example, if your name is Tom, the name of your turtle variable should be tom). At every step, you should choose an angle between 0 and 360 degrees at random and have your turtle turn right by that angle and take 10 steps. For example, to make Tom's turtle turn a random angle and walk 10 steps, you could write:
    	   angle = randrange(0,360)
    	   tom.right(angle)
    	   tom.forward(10)
    	 


    Be sure to include code to close the window following a final mouse click. Points will be deducted if the grader's computer crashes. To do this with turtle graphics, use:
    	   scr.exitonclick()
    	 
  4. Due Date: 10 November
    Write a function that uses turtle graphics to draw a petal. Your function should take a turtle as its input parameter and draw a single petal. For example,

    Draw a flower by repeatedly calling your function. Your flower should have at least 10 petals. For example,

    (Note: you can change the color that your turtle, using the function, color(). For example, if you turtle is called flower to change it's color to purple, write flower.color("purple").)



    Be sure to include code to close the window following a final mouse click. Points will be deducted if the grader's computer crashes. To do this with turtle graphics, use:
    	   scr.exitonclick()
    	 
  5. Due Date: 10 November
    Implement the following pseudo-code in a Python program.
    	   function leaf(turtle)
    	      Move turtle forward 50 steps
    	      Rotate turtle left 100 degrees
    	      Move turtle forward 30 steps
    	      Rotate turtle left 50 degrees
    	      Move turtle forward 30 steps
    	      Rotate turtle left 100 degrees
    	      Move turtle forward 50 steps
    	      Rotate turtle right 160 degrees
    	
               function stem(turtle)
    	      Rotate turtle right 95 degrees
    	      Move turtle forward 70 steps
    	      Rotate turtle left 5 degrees
    	      Move turtle forward 70 steps
    	
    	   function main()
    	      create a screen
    	      create a turtle
    	      loop four times
    	         leaf(turtle)
    	      stem(turtle)
    	      wait for click and close
    	 


    Be sure to include code to close the window following a final mouse click. Points will be deducted if the grader's computer crashes. To do this with turtle graphics, use:
    	   scr.exitonclick()
    	 

Problem Set 9

Submit the following programs via Blackboard:

  1. Due Date: 24 November
    Write a program that measures strings in terms of the length of your name. For example, if your first name is "Kate", you would use the length of the string "Kate" which is 4. If your name is "Daniel", you would use the length 6.

    Your program should print out your name to the screen and then ask the user to enter a string. You should then print out how long the string is in terms of the length of your name (that is, the length of the user's string divided by your length). For example:

    	   The measuring string is "Kate"
    	   Please enter a string:  Hello world
    	   Your string is 2.75 Kate's long.
             

    While, if your name was Daniel, your program would look like:

    	   The measuring string is "Daniel"
    	   Please enter a string:  Hello world
    	   Your string is 1.8333 Daniel's long.
             
  2. Due Date: 24 November
    Write a program that uses your full name and prints it to the screen, cycling through the string. You must store your name in a string and use a loop to print out the answer. For example, if your name was "Herbert H. Lehman", the print out would be:
    	   H e r b e r t   H .   L e h m a n
    	   e r b e r t   H .   L e h m a n H
    	   r b e r t   H .   L e h m a n H e
    	   b e r t   H .   L e h m a n H e r
    	   e r t   H .   L e h m a n H e r b
    	   r t   H .   L e h m a n H e r b e
    	   t   H .   L e h m a n H e r b e r 
    	   H .   L e h m a n H e r b e r t 
    	   H .   L e h m a n H e r b e r t   
    	   .   L e h m a n H e r b e r t   H 
    	   L e h m a n H e r b e r t   H .  
    	   L e h m a n H e r b e r t   H .   
    	   e h m a n H e r b e r t   H .   L 
    	   h m a n H e r b e r t   H .   L e 
    	   m a n H e r b e r t   H .   L e h 
    	   a n H e r b e r t   H .   L e h m 
    	   n H e r b e r t   H .   L e h m a
             
  3. Due Date: 24 November
    Make a list of at least 5 different places that you like to visit. Your program should print out the list regularly and vertically. For example:
    	  ['San Francisco', 'Christchurch ', 'Sydney       ', 'Bangkok      ', 'Copenhagen   ']
    	  S  C  S  B  C  
    	  a  h  y  a  o  
    	  n  r  d  n  p  
    	     i  n  g  e  
    	  F  s  e  k  n  
    	  r  t  y  o  h  
    	  a  c     k  a  
    	  n  h        g  
    	  c  u        e  
    	  i  r        n  
    	  s  c           
    	  c  h           
    	  o
    	

    Hint: Make sure all place names are the same length. If they are not add spaces to the shorter names to make them all equal length.

    Hint: If you have a list referenced by the variable places, then places[0] refers to the first place in your list, in this case 'San Francisco'.

    Hint: Also, places[0][i] refers to the character at index i of the first word in your list. Example: places[1][3] refers to the letter 'i' in the string 'Christchurch'.

  4. Due Date: 24 November
    Write a program that asks for a list of names and a list of cities, as well as a file name. Your program should use the file as a template to generate letters customized with the names and cities entered. You should generate a letter for each name/city pair on the inputted lists. The customized letters should replace every the **INSERT NAME HERE** with the name and **INSERT CITY HERE** with the address.

    For example, if the file inputTemplate.txt contained:

    	   New York, New York
    	   11 March 2013
    	
    	   **INSERT NAME HERE**
    	   **INSERT CITY HERE**
    
    	   Dear **INSERT NAME HERE**,
    
    	   Thank you for your service to New York City, and,
    	   in particular, to the education of its residents.
    	   Those in **INSERT CITY HERE** appreciate it!
    
    	   Best wishes to **INSERT NAME HERE** and your family,
    
    	   --CUNY
             

    A sample run of the program would be:

    	   Please enter the name of the template file:  inputTemplate.txt
    	   Please enter names of recipients seperated by commas:  Herbert H. Lehman, Bernard M. Baruch, Fiorello H. LaGuardia
    	   Please list cities seperated by commas:  Bronx NY, New York NY, Queens NY 
    		
    	   Your customized letters are below:
    	   
    	   New York, New York
    	   11 March 2013
    	
    	   Herbert H. Lehman
    	   Bronx NY
    
    	   Dear Herbert H. Lehman,
    
    	   Thank you for your service to New York City, and,
    	   in particular, to the education of its residents.
    	   Those in Bronx NY appreciate it!
    	   
    	   Best wishes to Herbert H. Lehman and your family,
    	   
    	   --CUNY
    	
    	
    	
    
    	   New York, New York
    	   11 March 2013
    	
    	   Bernard M. Baruch
    	   New York NY
    
    	   Dear Bernard M. Baruch,
    
    	   Thank you for your service to New York City, and,
    	   in particular, to the education of its residents.
    	   Those in New York NY appreciate it!
    
    	   Best wishes to Bernard M. Baruch and your family,
    
    	   --CUNY
    	
    
    
    	   
    	   New York, New York
    	   11 March 2013
    	
    	   Fiorello H. LaGuardia
    	   Queens NY
    
    	   Dear Fiorello H. LaGuardia,
    
    	   Thank you for your service to New York City, and,
    	   in particular, to the education of its residents.
    	   Those in Queens NY appreciate it!
    	   
    	   Best wishes to Fiorello H. LaGuardia and your family,
    	   
    	   --CUNY	
             
  5. Due Date: 24 November
    Write a program that asks the user for the name of a file and a line length. The line length must be between 10 and 20 (inclusive). Your program should then "wrap" all lines that are longer than line length, and print the result on the screen. For example, if the file contains:
    	   01234567890123456789012345
    	   This line has more than 20 characters.
    	   This one has less 
    	   And this one has lots, lots, lots, more than 20 characters!
             

    A sample run of your program would look like:

    	   Please enter a file name             : myspecialfile.txt
    	   Please enter a number between 10 & 20: 9
    	   Invalid input, please try again...
    	   Please enter a number between 10 & 20: 21
    	   Invalid input, please try again...
    	   Please enter a number between 10 & 20: 20
    	   Here is your output formated to a max of 20 characters per line:
    	   01234567890123456789
    	   012345	
    	   This line has more t
    	   han 20 characters.
    	   This one has less 
    	   And this one has lot
    	   s, lots, lots, more 
    	   than 20 characters!
             

    Another sample run of your program would look like:

    	   Please enter a file name             : myspecialfile.txt
    	   Please enter a number between 10 & 20: 15
    	   Here is your output formated to a max of 15 characters per line:
    	   012345678901234
    	   56789012345	
    	   This line has m
    	   ore than 20 cha
    	   racters.
    	   This one has le
    	   ss 
    	   And this one ha
    	   s lots, lots, l
    	   ots, more than 
    	   20 characters!
             

    Hint: break the problem in to parts: first write a program that will print lines from a file to the screen. Then modify your initial program to only print lines up to the length selected. And, finally, to finish the program, then add in the code that breaks up the lines according to the length selected.


Problem Set 10

Submit the following programs via Blackboard:

  1. Due Date: 15 December
    Translate the following pseudocode into a python function:
    	   algorithm fibonacci(n)
    	      if n is 0 or a negative number
                     return 0
    	      else if n < 2
    		 return 1 
    	      else
    	         return fibonacci(n-1)+fibonacci(n-2)
    	 
    Include in your submitted program a main() function that prompts the user for a number, calls your function, and then displays the answer.
  2. Due Date: 15 December
    Is it likely that a monkey at a typewriter, randomly choosing letters, could write Hamlet? Write a simulation to answer this question. That is, write a program that will randomly generate 100,000 strings of length 5 and count and print out the number of strings that match "to be". You may assume that your strings are lower case letters and spaces only. That is, when you generate your string, you are choosing from the 27 characters:
    	  [' ','a','b','c','d',...'y','z']
    	
  3. Due Date: 15 December
    Write a simulation of the rolling of 2 six-sided dice. Your program should have a function oneRoll() that returns the sum of rolling your dice. You may assume that each of the six sides is equally likely to be rolled (that is, the dice are "fair"). Run your simulation 10,000 times and report the frequency that each sum occurred. A sample run of your program should look something like (but not identical due to the randomness of the simulation):
    	  2 : 292
    	  3 : 536
    	  4 : 810
    	  5 : 1100
    	  6 : 1428
    	  7 : 1631
    	  8 : 1439
    	  9 : 1100
    	  10 : 825
    	  11 : 543
    	  12 : 296
    	
  4. Due Date: 15 December
    Modify selectionSort (from Chapter 13 of the book or Zelle's website) to sort a list, but with the twist: your name always come first and the rest of the names are in order "wrapping around" the alphabet. For example, if your starting list is:
    	  Eric, Sameh, Ali, Tom, Kostos, Tony, Katherine
    	
    And your name is Kostos, the ending list would be:
    	  Kostos, Sameh, Tom, Tony, Ali, Eric, Katherine
    	
    If your name is Eric, the ending list would be:
    	  Eric, Katherine, Kostos, Sameh, Tom, Tony, Ali
    	

    In your submitted file, include a main() function that prints out your name and demonstrates that the sort algorithm works.

    Hint: you only need to change the "comparison" part of the sort, but you will need to make it more complicated. If you are trying to figure out which of two strings, say string1 and string2 come first, you need to compare them first to your name. If both are before or both are after your name, you can compare in the same way as before. If one of the strings comes before your name and the other after your name, you will have additional comparisons.

  5. Due Date: 15 December
    Write the python code for the algorithm below: (from rosettaCode.org)
    	   algorithm stoogesort(L, i, j)
    	      if L[j] < L[i] then
                     swap L[i] and L[j]
    	      if j - i > 1 then
    	         t := (j - i + 1)/3
    	         stoogesort(L, i, j-t)
    	         stoogesort(L, i+t, j)
    	         stoogesort(L, i, j-t)
    	      return L
    	

    In your submitted file, include a main() function that creates a list L containing integers in some random order and calls stoogesort with the call stoogesort(L, 0, length(L)-1) and then prints out L.


Here's xkcd on the simplicity of python:

(This file was last modified on 21 December 2012.)