General Notes

Submit the following programs via Blackboard:

  1. Due Date: 31 January Reading: Chapters 1 & 2
    Write a program that prints your name to the screen 20 times.
  2. Due Date: 3 February Reading: Chapters 1 & 2
    Modify the chaos program (available from the author's website) so that it prints out 15 values instead of 10.
  3. Due Date: 4 February Reading: Chapters 1 & 2
    Modify the avg2.py program (Section 2.5.3 from the book and from the book's webpage) to find the average of four exam scores.
  4. Due Date: 5 February Reading: Chapters 1 & 2
    Write a program that converts weights measured in pounds to kilograms. One pound is approximately 2.2 kilograms. (Hint: see the convert.py program from the book's webpage.)
  5. Due Date: 6 February Reading: Chapters 1 & 2
    Write a program that will print out a conversion table for dollars and another currency. The currency you should use is based on the first letter of your first name:
    If your name begins with...Use the currency:$1 is worth:
    AAfghan Afghani (AFN)51.08
    BBangladeshi Taka (BDT)80.58
    CCosta Rican Colon (CRC)499.38
    DDanish Krone (DKK)5.67
    EEuro (EUR)0.77
    FFalkland Island Pound (FKP) 0.62
    GGuatemalan Quetzal (GTQ)0.13
    HHungarian Forint (HUF) 0.00456
    IIndonesian Rupiah (IDR)9639.99
    JJapanese Yen (JPY)83.85
    KKenyan Shilling (KES)0.0116
    LLebanese Pound (LPP)0.000664
    MMoroccan Dirham (MAD)0.118
    NNepalese Rupee (NPR)0.0115
    OOmani Rial (OMR)0.384
    PPolish Zloty3.11
    QQatari Riyal (QAR)0.275
    RRussian Ruble (RUB)30.80
    SSomali Shilling (SOS)0.000619
    TThai Baht (THB)30.59
    UUkrainian Hryvna (UAH)8.10
    VVenezuelan Bolivar (VEH)4.30
    WSamoan Tala (WST)2.27
    XEast Caribbean Dollar (XCD)2.70
    YYemeni Rial (YER)0.00477
    ZZimbabwean Dollar (ZWD)361.90
    For example, if your first name is Eric, you would use Euros. From the table above, we have that one dollar is worth 0.77 euros. You should begin the program by printing out your name. On each line, your program should write the number of dollars and the corresponding number of your currency. For example:
    Eric's converting program
    		
    Dollars:    Euros:
    1           0.77
    2           1.53
    3           2.3
    4           3.07
    5           3.84
    		
    Your program should print the information for 1, 2, ..., 10 dollars. You do not need to worry about formatting (we will talk about that more in Chapter 5), but you do need to calculate all 10 entries. Hint: modify the program from the Lab 2.
  6. Due Date: 10 February Reading: Chapter 3
    Write a program that asks the user for time in days and returns the number of years and days (assume that it is not a leap year and there are 365 days in a year). For example, if the user entered, 400, your program would output, 1 years 35 days. Note that you should use "years" and "days" for all inputs (even in the cases where it is grammatically incorrect).
  7. Due Date: 11 February Reading: Chapter 3
    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.25 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.
    		
  8. Due Date: 12 February Reading: Chapter 3
    Built into python is a module for simple drawing, called Turtle graphics. To use the Turtle graphics module, you must import it, just like with the math module. Some basic turtle commands are:
    • left(degs): turns your turtle left degs degrees
    • right(degs): turns your turtle left degs degrees
    • forward(s): moves your turtle forward s steps
    • backward(s): moves your turtle forward s steps
    The following draws a 4-sided figure or square:
    import turtle
    
    def main():
        daniel = turtle.Turtle()    #Set up a turtle named "daniel"
        myWin = turtle.Screen()     #The graphics window
    
        #Draw a square
        for i in range(4):
            daniel.forward(100)     #Move forward 10 steps
            daniel.right(90)        #Turn 90 degrees to the right
    
        myWin.exitonclick()         #Close the window when clicked
        
    main()		
    		
    Modify this program to draw a 6-sided figure or hexagon. Make sure to include the standard introductory comments at the beginning of your program as well as to change the name of the turtle to your name.
  9. Due Date: 16 February Reading: Chapter 3
    Write a program to calculate how much money you would have if your money doubled every day. Your program should ask the user for the number of days and display the amount of money for each day. The starting amount of money is $1. Here is a sample interaction:
    Please enter the number of days:  8
    Day 1:  $1
    Day 2:  $2
    Day 3:  $4
    Day 4:  $8
    Day 5:  $16
    Day 6:  $32
    Day 7:  $64
    Day 8:  $128
    		
  10. Due Date: 17 February Reading: Chapter 3
    Use turtle graphics to draw a multi-pointed star. Your star must have at least 10 points (or outer corners) and the lines must cross in the middle, and the starting and ending points should be the same. Here are some examples:
  11. Due Date: 19 February Reading: Chapter 4
    Using Zelle's graphics module, draw a snowman with your initial (the first letter of your first name) on him. Your display should contain 3 or more circles forming the body of the snowman, as well as a mouth and eyes. In the center of the snowman, you should include your initial or name. You must use at least 3 different colors in your drawing.
  12. Due Date: 20 February Reading: Chapter 4
    Modify the bouncing ball example from Lab 3 so that the ball bounces a little bit less each time. At the end, it should appear to roll across the x-axis. (Hint: the only change needed is to the height of the ball. How can you make the height get smaller as time increases?)
  13. Due Date: 21 February Reading: Chapter 4
    Write a program that displays a graphics window that shows the shades of one of the basic colors: red, green, or blue. Every color is represented on screen by the amount of red, green, or blue it has (called the "rgb" value):

    starting value:color_rgb(5,0,0)color_rgb(0,51,0)color_rgb(0,0,59)
    ending value:color_rgb(255,0,0)color_rgb(0,255,0)color_rgb(0,0,255)
    shape:a circle whose x-coordinate and radius increase each step a circle whose x and y coordinates increase each step skinny, full height rectangles whose x coordinate increases each step

    Choose your favorite color of red, green, and blue. Let startingValue be the last two digits of your Blackboard username (long number usually starting with 2011....) and set your favorite part of the color to that number (and the other two values to zero, as above). So, for example, if the last two digits of your username were 59 and your favorite color is blue, you would use startingValue = 59 and color_rgb(0,0,startingValue). Your program should display at least 100 objects (circles, rectangles, or what ever you choose). The first object should be your favorite color with startingValue. The last object should have the same color with startingValue replaced by 255. The object should get increasingly lighter. Hint: Use (255-startingValue) objects and then you can let the color increase by 1 each time. To give a smooth transition, use the setOutline() function on your object to make the outline color match (the default is white outlines on objects).

  14. Due Date: 22 February Reading: Chapter 4
    Modify the program triangle.py from the textbook (also available at the author's website) to draw a 5-sided polygon or pentagon from 5 points clicked on by the user.
  15. Due Date: 23 February Reading: Chapter 4
    Modify the bouncing ball problem (from Lab 3) to ``wrap'' around the screen twice. That is, when the ball gets to the right end of the screen, have it continue on the left hand side. Hint: use the remainder operator (%) to make sure that the y-coordinate is always between 0 and the width of the window.
  16. Due Date: 3 March Reading: Chapter 4
    Write a program that takes twenty mouse clicks from the user and draws a polygon whose corners, or vertices, are the clicked points. Hint: use the program you wrote for Lab 4 and add a line from the last point clicked to the first point clicked.
  17. Due Date: 4 March Reading: Chapter 4
    Write a program that takes two mouse clicks from the user and draws rectangle whose corners include the two points clicked.
  18. Due Date: 5 March Reading: Chapter 5
    Modify the decodeMessage.py program from Lab 5 to use an offset entered by the user to encrypt the message. The program in the lab encrypts each character by the character 2 places ahead in the alphabet. Your program should ask the user for the offset and encrypt their string by replacing each character by one that is offset places ahead in the alphabet.
  19. Due Date: 6 March Reading: Chapter 5
    Write a program that asks the user for a list of prices of items purchased and prints out each price and the total, formatted nicely. Here is a sample run of the program:
    Please enter the prices:  2.34, .99, 100, 81.05, 90
    		
    Your receipt:
             2.34
             0.99
           100.00
            81.05
            90.00
    ----------------
    Total: 274.38	
    		
    (Hint: use the format() statement discussed in Chapter 5.)
  20. Due Date: 7 March Reading: Chapter 5
    Make a list of at least 5 different places that you like to visit (these should be places that you would like to visit and not identical to others' lists or it will be picked up by the cheat-checking programs).

    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: if you have a list of strings, places, then places[0][i] refers to the ith character of the first word in your list. Start by printing out the first place name vertically, then add in the others.
  21. Due Date: 10 March Reading: Chapter 5
    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
    
    Too similar to the one above?
  22. Due Date: 11 March Reading: Chapter 5
    Write a program that asks for a list of names and a list of cities, as well as a file. 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/address pair on the inputted lists. The customized letters should replace every the **INSERT NAME HERE** with the name and **INSERT ADDRESS HERE** with the address.

    For example, if the file inputTemplate.txt contained:

    						New York, New York
    						11 March 2013
    	
    **INSERT NAME HERE**
    **INSERT ADDRESS 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 ADDRESS 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:  Herbert H. Lehman, Bernard M. Baruch, Fiorello H. LaGuardia
    Please list addresses:  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	
    		
    		
  23. Due Date: 12 March Reading: Chapter 5
    Write a program that asks the user for the name of a file and a line length. 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!
    		
    and the user entered the length of 20, all lines longer than 20 would be wrapped to the next 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!
    		
    Hint: break the problem in to parts: first write a program that will print lines from a file to the screen (see Lab 6). Then modify your initial program to only print lines up to the length entered. And, to finish the program, then add in the code that prints lines that are longer than the length entered.
  24. Due Date: 13 March Reading: Chapter 5
    Write a program that asks the user for a name of a file and then prints to the screen the number of characters and lines in the file. Hint: see
    Lab 6.
  25. Due Date: 14 March Reading: Chapter 5
    Write a program that will take price data for stocks and print it graphically to the screen. Your program should begin by asking the user for the file name. It should then create a graphics window and plot the date versus price. You may assume that the file has the following format:
    "date","close","volume","open","high","low"
    "16:00","720.11","1,919,799","720.71","723","716.68"
    "2012/12/19","720.1100","1918493.0000","720.7100","723.0000","716.6800"
    "2012/12/18","721.0700","3004838.0000","716.6000","729.1000","715.0500"
    "2012/12/17","720.7800","3034558.0000","705.5000","738.2800","704.0200"
    "2012/12/14","701.9632","2129893.0000","699.1700","707.8200","698.4300"
    "2012/12/13","702.7000","3443866.0000","715.9200","716.4750","699.5500"
    "2012/12/12","697.5600","2425774.0000","699.2300","703.5100","693.4800"
    
    (file from: http://www.nasdaq.com/symbol/goog/historical)

    where the first line describes the columns and each subsequent line of the file describes the stock price on a given day, separated by commas. Note that the historical data (from Nasdaq) lists the information in reverse chronological order, while graphs of financial data always start with the oldest date first. Your graph should have the oldest date on the left, progressing to the newest date on the right.

    Hint: You may assume that the price of any stock is positive and below $1000, so, the y-coordinates of your window should between 0 and 1000. You may also assume that you are graphing data from one year. There are at most 52 weeks * 5 business days/week = 260 days per year, so your x-coordinates should range from 0 to 260 (or a bit extra to make it look nice).

    Here are some sample files you can use to test your program: (from http://www.nasdaq.com/):

    And a simple plotting of the data:

  26. Due Date: 17 March Reading: Chapter 6
    Modify the program from Lab 7 to allow the user to enter two different mathematical functions to be graphed. Your program should then display both on in the graphics window.
  27. Due Date: 18 March Reading: Chapter 6
    (From Chapter 6, #1, Page 196) 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!
    	   Whith 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)
  28. Due Date: 19 March Reading: Chapter 6
    Fill in the missing function definitions for this program:
    def main():
    	w = setUp()			#Creates and returns a graphics window
    	x1,y1,x2,y2 = userInput()	#Asks user for 4 inputs and returns numbers entered
    	displayLine(w,x1,y1,x2,y2)	#Draws a line from (x1,y1) to (x2,y2) on window w
    	conclusion(w)			#Gets a mouse click and closes window w
    main()
    That is, write the functions setUp(), userInput(), displayLine() and conclusion(). Include all functions, including the main() above in the file you submit.
  29. Due Date: 20 March Reading: Chapter 6
    Write a function that takes as a parameter a list of strings and returns a list containing the lengths of each of the strings. That is, if the input parameter is ["Daniel","Nashyl","Orla", "Simone","Zakaria"], your function should return [6, 6, 4, 6, 7]. The file you submit should include a main() function that demonstrates that your function works.
  30. Due Date: 21 March Reading: Chapter 6
    Write a function that uses turtle graphics to draw a pedal. Your function should take a turtle as its input parameter and draw a single pedal. For example,

    Draw a flower by repeatedly calling your function. Your flower should have at least 10 pedal. 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").)


  31. Due Date: 14 April Reading: Chapter 7
    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 that 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. (Hint: see Lab 8.)
  32. Due Date: 15 April Reading: Chapter 7
    Write a function that takes as two parameters: 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, return a negative number (since your calculator does not handle inputs that high).

    You should include in the file a main() that calls your function several times to demonstrate that it works.

  33. Due Date: 16 April Reading: Chapter 7
    Write a program that allows the user to choose which currency they would like to convert. Using the chart from #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 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 
    
  34. Due Date: 17 April Reading: Chapter 7
    Write a program that asks a user to enter a list of 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, 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. To make the receipt look nice, you can use format() and rjust() functions.
  35. Due Date: 18 April Reading: Chapter 7
    Modify the decodeMessage.py program from Lab 5 which encodes messages with lower case letters to one that can also encode messages with upper case letters and numbers.

    For example,

    This program decodes a textual message
    Each character is replaced with the character 2 places ahead in the alphabet.
    The characters "'", ".", "(", ")", and " " are not decoded.
    
    Please enter the message to decode: 250 Bedford Park Blvd W, 10468
    
    The decoded message is: 472 Dgfhqtf Rctm Dnxf Y, 32680
    		

    (Hint: try the original program on the message below. What are the differences in the encoded messages it produces and the output above?)


  36. Due Date: 21 April Reading: Chapter 8
    Write a program that asks the user to enter a number between -10 and 10. If they enter a number out of range, print a message that the number is out of range and prompt them again for a number between 1900 and 2012. When the user enters a number in range, print the number to the screen and end the program.
  37. Due Date: 22 April Reading: Chapter 8
    Write a guessing word program. First, pick a word, name, or phrase for the user to guess (make sure that it is different from others' words so the cheat-checking programs don't accidently match your program to others and that the word is not obscene (since it will be flagged by the Blackboard system)).

    While the user has not guessed the word, print out two messages: one that says whether the message that was entered is too short, just right, or too long. Your second message should tell the user if there guess was before or after in the ordering of strings. Your game should continue until they guess your secret string and then should print a message congratulating them. Hint: see Lab 9.

  38. Due Date: 23 April Reading: Chapter 8
    Write a program that reads in a text file, infile.txt, and prints out all the lines in the file to the screen until it encounters a line with fewer than 5 characters. Once it finds a short line (one with fewer than 4 characters), the program stops.
  39. Due Date: 24 April Reading: Chapter 8
    Write a program that asks the users to enter a sequence of amounts to credit or debit to their account. Your program should continue to process their amounts as long as their balance is positive. If at any point they have a negative balance, print a message and end the program. If you are able to process all the amounts, print the final balance to the screen before ending the program. You may assume that their beginning balance is $1000.

    One sample run of the program:

    Please enter the amounts to credit/debit from your account, separated by commas:  
    -500,100.01,-1000,333.33,85.02,-200
    	
    Beginning Balance:  $1000.00
    	
    Amount: -500
    Balance: $500.00
    Amount: 100.01
    Balance: $600.01
    Amount: -1000
    	
    Your balance has dropped below 0!  Please contact the bank immediately!
    	

    Another sample run of the program:

    Please enter the amounts to credit/debit from your account, separated by commas:  
    5.00,123.01,-600,7.89,-99.99
    	
    Beginning Balance:  $1000.00
    	
    Amount: 5.00
    Balance: $1005.00
    Amount: 123.01
    Balance: $1128.01
    Amount: -600
    Balance: $528.01
    Amount: 7.89
    Balance: $535.90
    Amount: -99.99
    Balance: $435.91
    	
    Your final balance is:  $435.91
    
  40. Due Date: 25 April Reading: Chapter 8
    Write a program for the game 'Hangman.' Choose a secret word for the user to guess. Then, prompt the user to guess a letter. If they get it right, the letter should be filled in the word, if they get it wrong, add to mistake count. The program should continue to prompt the user and fill in letters until the user has all the letters in the word, or the mistake count reaches 6.

    For example,

    Welcome to Hangman!
    The secret word has 6 unknown letters:  _ _ _ _ _ _
    Guess a letter:  K
    There is no K, mistake count = 1
    The secret word has 6 unknown letters:  _ _ _ _ _ _
    Guess a letter: A
    There is no A, mistake count = 2
    The secret word has 6 unknown letters:  _ _ _ _ _ _
    Guess a letter: T
    There is a T, mistake count = 2
    The secret word has 5 unknown letters:  _ _ T _ _ _
    Guess a letter: H
    There is a H, mistake count = 2
    The secret word has 4 unknown letters:  _ _ T H _ _
    Guess a letter: E
    There is no R, mistake count = 3
    The secret word has 4 unknown letters:  _ _ T H _ _
    Guess a letter: R
    There is no R, mistake count = 4
    The secret word has 4 unknown letters:  _ _ T H _ _
    Guess a letter: I
    There is no 1, mistake count = 5
    The secret word has 4 unknown letters:  _ _ T H _ _
    Guess a letter: N
    There is no R, mistake count = 6
    The secret word has 3 unknown letters:  _ _ T H _ N
    Guess a letter: P
    There is a P, mistake count = 6
    The secret word has 2 unknown letters:  P _ T H _ N
    Guess a letter: Y
    There is a Y, mistake count = 6
    The secret word has 1 unknown letters:  P Y T H _ N
    Guess a letter: O
    There is a O, mistake count = 6
    The secret word has 5 unknown letters:  P Y T H O N
    
    Congratulations!  You won the game!
    

    To make the programming simpler, you may choose a secret word where every letter occurs at most once, as long as your word has over 5 letters. As before, make sure that your word is different from others' words so the cheat-checking programs don't accidently match your program and that the word is not obscene (since it will be flagged by the Blackboard system).


  41. Due Date: 2 May Reading: Chapter 9
    Modify the stock simulation from Lab 10 to calculate and print the average ending value of money.
  42. Due Date: 4 May Reading: Chapter 9
    Use turtle graphics to simulate a random walk of 1000 steps long. In your program, create a turtle named for you (for example, if your name is Tom, your 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 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)
    		
  43. Due Date: 6 May Reading: Chapter 9
    Is it likely that a monkey at a typewriter, randomly choosing character, 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 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']
  44. Due Date: 8 May Reading: Chapter 9
    Write a simulation of the rolling of 2 six-sided dice. Your program should have a function that 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	
    	
  45. Due Date: 10 May Reading: Chapter 9
    Write a simulation of the rolling of 2 dice: 1 four-sided die and 1 eight-sided die. That is, the first die has four sides, and you can roll, with equal probability: 1,2,3, or 4. The second die has eight sides, and you can roll with equal probability: 1, 2, 3, 4, 5, 6, 7, or 8. Your program should have a function that oneRoll() that returns the sum of rolling your dice. 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 : 308
    3 : 629
    4 : 932
    5 : 1231
    6 : 1230
    7 : 1242
    8 : 1343
    9 : 1198
    10 : 938
    11 : 617
    12 : 332
    	

  46. Due Date: 12 May Reading: Chapter 13
    Modify the selectionSort (from Chapter 13 of the book or Zelle's website) to sort lists of numbers backwards. For example, the list, [125,4,145,59,168,34,14,42], should be sorted as: [168,145,125,59,42,34,14,4].

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

  47. Due Date: 13 May Reading: Chapter 13
    The program below draws nested hexagons, via tail recursion:
    from turtle import *
    def mystery2(t,x):
    	for i in range(6):
    		forward(x)
    		right(60)
    	if x > 0:
    		mystery2(t,x-10)
    t = Turtle()
    mystery2(t,50)	
    	
    Modify the program so that it draws nested triangles instead.
  48. Due Date: 14 May Reading: Chapter 13
    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.

  49. Due Date: 15 May Reading: Chapter 13
    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
    
    stoogesort(L, 0, length(L)-1)
    
    In your submitted file, include a main() function that demonstrates that the sort algorithm works.
  50. Due Date: 16 May Reading: Chapter 13
    Implement the following pseudocode in python:
    algortihm fern(turtle, size)
        if size > 4
        	Move turtle forward size/25 steps
            Turn turtle 90 degrees left
            fern(turtle, size*0.3)
            Turn turtle 180 degrees right        
            fern(turtle, size*0.3)
            Turn turtle 90 degrees left
            fern(turtle, size*0.85)
            Move turtle backward size/25 steps
    
    Include a main program that calls your fucntion with a turtle and size of 500
    (from http://nakkaya.com/2010/01/09/a-simple-turtle-graphics-implementation-in-clojure/). Note: starting at 500 will only draw a small fern. If you would like to match the image from the above site, you will need an input parameter of 1500 and a lot of patience while it draws.

    Hints:

    • Have your turtle turn 90 degrees to the left so that the "fern" will draw upwards.
    • You can have the turtle draw faster by using the speed() function from the turtle module.
    • Implement the algorithm above. Do not search the internet for python code and submit that. Not only is that plagiarism (and will earn you a grade of 0 for the assignment), it will likely not implement the same fern as above, since there are many, many different variants of this fractal drawing.
Here's xkcd on the simplicity of python:

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