CSC111 Homework 5 2014

From dftwiki3
Revision as of 11:30, 27 February 2014 by Thiebaut (talk | contribs) (Problem #2)
Jump to: navigation, search

--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.





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

# 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", animal, "lives on the farm"


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


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.

# 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).

Problem #3


Will be provided later...