CSC111 Homework 5 2014
--D. Thiebaut (talk) 22:05, 25 February 2014 (EST)
This assignment is due at midnight on Thursday, 3/6/14 at midnight. You can work on it individually or work in pair. You are free to pick your partner if you decide to work in pair, in which case you should make sure that both names and 2-letter Id appear in the header of your program.
Contents
Problem #1: Random poems
This problem touches on many different Python constructs we have seen so far.
Run the following program that will remind you of the Rock, Scissors, Paper game in the way it uses a random animal.
# hw5a.py
# firstName lastName
# this program prints a random word every time it is run
#
from random import choice
from random import seed
seed()
# create a list of animals
farm = [ "pig", "cat", "dog", "horse", "donkey", "monkey", "snake", "fly" ]
# figure out how many animals are on the farm
animal = choice( farm )
# select the animal at that index and print its name
print( "the %s lives on the farm" % animal )
Create this program with Idle. Observe that every time you run it, the output is unpredictable.
Using this facility, write a program that creates random poems. In our case, a poem will be just one line long, and will contain a subject, an adverb, a verb, a location, and a time. We'll see an example very shortly that will clarify this idea.
Assume that we have a list of subjects, a list of adverbs, a list of verbs, a list of locations, and a list of times, as follows:
subjects = [ "The cat", "Cathy", "Oprah", "Paris Hilton" ]
adverbs = [ "never", "sometimes", "usually", "amazingly", "voraciously" ]
verbs = [ "likes to sleep", "eats mice", "runs" ]
locations = [ "around the pond", "on the sofa", "in Duckett" ]
times = [ "at night", "early in the morning", "every Sunday afternoon", "whenever possible" ]
If we pick a random word from each list and "glue" all the words together, we end up with a one-line poem. For example Paris Hilton amazingly eats mice in Duckett House every Sunday afternoon.
Your assignment is to write a program that declares a different series of lists (lists should not have the same number of words), and asks the user how many one-line poems she would like to see. Your program will then print that many different poems.
Here is an example of how your program should work (the user input is underlined):
Welcome to the Random Poems of the Day! How many poems would you like me to generate for you? 3 Here are your 3 poems: Paris Hilton amazingly eats mice in Duckett every Sunday afternoon The cat never runs on the sofa early in the morning Cathy sometimes runs on the sofa at night
Use your imagination when you create the lists of words, please!
Requirements
If the user enters a number that is 0 or negative it will keep on prompting the user, and will accept only integers greater than 0.
Submission
Store your program in a file called hw5a.py and submit it to this URL: http://cs.smith.edu/~dthiebaut/111b/submit5.php
Problem #2
Video Tips
Setup
This assignment requires you to first create a Python file with some variables. The contents of the file is shown below.
Copy it into a Python program which you must call hw5data.py. Don't worry if you do not understand fully what this program does. It uses advanced features we haven't seen yet.
# hw5data.py
# D. Thiebaut
#
WORDLENGTH = 5
GAPLENGTH = 3
array = []
sign = -1
for i in range(WORDLENGTH):
array.append( (i*5%7+1) )
for i in range(GAPLENGTH):
array.append( 0 )
for i in range(WORDLENGTH):
array.append( (i+1)*2 )
for i in range(GAPLENGTH):
array.append( 0 )
for i in range(WORDLENGTH):
array.append( sign*(i*7%9+1) )
sign = -sign
for i in range(GAPLENGTH):
array.append( 0 )
#print( "array = ", array )
#print()
noSamples = len( array )
firstWordIndex = 0
secondWordIndex = noSamples//3
thirdWordIndex = (2*noSamples)//3
It looks like this program does nothing, but actually it creates several variables that your homework program, the one you have to write for this assignment, will use.
Create a new program called hw5b.py and save it in the same directory where you have saved hw5data.py.
Enter the code below in hw5b.py.
# hw5b.py skeleton program
# D. Thiebaut
from hw5data import array
from hw5data import noSamples
from hw5data import firstWordIndex
from hw5data import secondWordIndex
from hw5data import thirdWordIndex
# display whole array
print( "array = ", array )
- Run hw5b.py
- Notice that it outputs
array = [1, 6, 4, 2, 7, 0, 0, 0, 2, 4, 6, 8, 10, 0, 0, 0, -1, 8, -6, 4, -2, 0, 0, 0]
- Your hw5b.py program knows what the variable array is because we have told it, at the top of the program, to import this variable from the program hw5data.py (we do not put the ".py" in the import statement).
- If you look closely at the array variable, it is a list of integers, and we clearly see 3 sequences of numbers separated by some 0s. For our purpose, this array will be a list of samples from a wave sound-file. It will be easier for this assignment to write a program that works with an array of numbers than with sound files on JES!
- You should probably make your program display the different variables it imports to understand what they represent (although I picked names that should help you figure out what they are used for.) Modify your hw5b.py program and add the following statements to it at the end of it.
print( "Number of samples = ", noSamples ) print( "index of first word = ", firstWordIndex ) print( "index of second word = ", secondWordIndex ) print( "index of third word = ", thirdWordIndex ) print() print( "First word = ", end = "" ) for i in range (firstWordIndex, secondWordIndex): print( array[i], end=" " ) print() print( "Second word = ", end = "" ) for i in range (secondWordIndex, thirdWordIndex ): print( array[i], end=" " ) print()
- Run the program.
- Make sure you understand what the output shows, and what the different variables contain.
Question 1
Remembering that the array represents a collection of sound samples for a sentence with three words, write a program that will clear the first and last word of the array.
- Remove the print statements above so that when your program runs it does not display the variable.
- Add some statements to your program so that it will clear the first and last word of the array.
- Your program should output this:
Original array = [1, 6, 4, 2, 7, 0, 0, 0, 2, 4, 6, 8, 10, 0, 0, 0, -1, 8, -6, 4, -2, 0, 0, 0] Clear all except Word 2= [0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 6, 8, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
- Note: Make sure that you use the variables provided to you by hw5data.py, and avoid using numbers in your for-loops, otherwise your program will not work when I test them with sequences that will be longer or shorter than the one shown here (see the Testing section below for more information on this).
Question 2
- Make your program replicate the second word and put it at the front and back of the array.
- Here's what your program should output.
Original array = [1, 6, 4, 2, 7, 0, 0, 0, 2, 4, 6, 8, 10, 0, 0, 0, -1, 8, -6, 4, -2, 0, 0, 0] Clear all except Word 2= [0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 6, 8, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Word 2 replicated = [2, 4, 6, 8, 10, 0, 0, 0, 2, 4, 6, 8, 10, 0, 0, 0, 2, 4, 6, 8, 10, 0, 0, 0]
Problem #3
The setup is the same as for Problem #2: You need the hw5data.py for your new program called hw5c.py to work.
Hw5c.py will have the same import statements at the top as hw5b.py does.
Your assignment is to make hw5c.py "move" the words around, so that the first word will move to Position 3, the second word to Position 1, and the third word to Position 2.
Here is what the output of your program should be:
Original array = [1, 6, 4, 2, 7, 0, 0, 0, 2, 4, 6, 8, 10, 0, 0, 0, -1, 8, -6, 4, -2, 0, 0, 0] Rearranged words= [2, 4, 6, 8, 10, 0, 0, 0, -1, 8, -6, 4, -2, 0, 0, 0, 1, 6, 4, 2, 7, 0, 0, 0]
Testing
To test your programs for Problem 2 and Problem 3, I will simply change the two variables at the top of hw5data.py and set them to different numbers. If your programs use variables and avoid using constants or literals in their for-loops, then your programs should still correctly work with the new sequence generated.
For example, here is how my solution programs work when I set WORDLENGTH and GAPLENTH in hw5data.py differently:
WORDLENGTH = 4 GAPLENGTH = 2
- the output of my solution hw5b.py becomes:
Original array = [1, 6, 4, 2, 0, 0, 2, 4, 6, 8, 0, 0, -1, 8, -6, 4, 0, 0] Clear all except Word 2= [0, 0, 0, 0, 0, 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0] Word 2 replicated = [2, 4, 6, 8, 0, 0, 2, 4, 6, 8, 0, 0, 2, 4, 6, 8, 0, 0]
- and the output of my solution hw5c.py becomes:
Original array = [1, 6, 4, 2, 0, 0, 2, 4, 6, 8, 0, 0, -1, 8, -6, 4, 0, 0] Rearranged words= [2, 4, 6, 8, 0, 0, -1, 8, -6, 4, 0, 0, 1, 6, 4, 2, 0, 0]
Grading
- 20 points out of 100 will now be given to good documentation and readability of the program.
- This means that you need to explain in the header what the program does, how it works (or what feature that is supposed to work is not working correctly--yes, that should be part of the documentation!)
- Add comments inside the code
- Highlight the different code sections
- Look at the solution programs for Homework 3 for inspiration, and in particular this solution program which was particularly well documented.
Submission
- Submit your 3 programs hw5a.py, hw5b.py, hw5c.py to this URL: http://cs.smith.edu/~thiebaut/111b/submit5.php
- If you are working in pair, submit only 1 set of programs, and make sure both names are in the header of the programs.