Difference between revisions of "CSC111 Homework 3"
(→Problem #2: More Sophisticated Poems (Optional and extra credit)) |
(→Problem #1: Random poems) |
||
Line 19: | Line 19: | ||
. | . | ||
− | # | + | # hw3a.py |
# 111c-xx | # 111c-xx | ||
# firstName lastName | # firstName lastName | ||
Line 80: | Line 80: | ||
Use your imagination when you create the lists of words, please! | Use your imagination when you create the lists of words, please! | ||
− | Store your program in a file called | + | Store your program in a file called hw3a.py and submit it as follows: |
− | submit hw3 hw3a.py | + | submit hw3 hw3a.py |
==Problem #2: More Sophisticated Poems== | ==Problem #2: More Sophisticated Poems== |
Revision as of 15:28, 10 February 2010
This assignment is due at midnight on Thursday, 2/18/10.
Problem #1: Random poems
It is sometimes necessary to select random items in a list. Generating random numbers is an easy task, and is can be done by using the randrange() function that is available in a standard Python program called random.py. Because this special program is part of the language, and available with all versions of python (for Linux, Windows, or the Mac), you do not need to physically get a copy of it as you did in the previous assignment. It is always there on the system, installed as part of the python language. All you have to do in your program is state that you want to use the function randrange() that is part of the random program.
Here is an example of how this works:
.
# hw3a.py
# 111c-xx
# firstName lastName
# this program prints a random word every time it is run
#
from random import randrange
def main():
# create a list of animals
farm = [ "pig", "cat", "dog", "horse", "donkey", "monkey", "snake", "fly" ]
# figure out how many animals are on the farm
animalCount = len( farm )
# pick a number at random that is between 0 (included) and
# the number of animals (excluded)
randomIndex = randrange( animalCount )
# select the animal at that index and print its name
print "the", farm[ randomIndex ], "lives on the farm"
main()
.
Create this program in your 111c-xx account on Beowulf (or cut and paste it from this Web page). Observe that every time you run it, the output is unpredictable.
Using this new possibility, 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. An example 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", "Mrs Christ", "Our CS prof", "Paris Hilton" ]
adverbs = [ "never", "sometimes", "usually", "amazingly", "voraciously" ]
verbs = [ "likes to sleep", "eats mice", "runs" ]
locations = [ "around the pond", "on the sofa", "in Duckett House" ]
times = [ "at night", "early in the morning", "every Sunday afernoon", "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: 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. In response the program will print that many different poems.
Here is an example of how your program should work:
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 House every Sunday afternoon The cat never runs on the sofa early in the morning Mrs Christ sometimes runs on the sofa at night
Use your imagination when you create the lists of words, please!
Store your program in a file called hw3a.py and submit it as follows:
submit hw3 hw3a.py
Problem #2: More Sophisticated Poems
Same assignment as before, but with the following restriction: the five lists of words are kept is a master list called collection.
subjects = [ "The cat", "Mrs Christ", "Our CS prof", "Paris Hilton" ] adverbs = [ "never", "sometimes", "usually", "amazingly", "voraciously" ] verbs = [ "likes to sleep", "eats mice", "runs" ] locations = [ "around the pond", "on the sofa", "in Duckett House" ] times = [ "at night", "early in the morning", "every Sunday afernoon", "whenever possible" ] collection = [ subjects, adverbs, verbs, locations, times ]
Note that collection is a list of lists.
Your assignment for this problem is to write your program so that it uses only collection and none of the other lists in its code. In other words, your challenge is to reproduce the same behavior as the program of Part 1, but the only list you can use in your code is collection.
Call your program hw3b.py and submit it as follows:
submit hw3 hw3b.py