Difference between revisions of "CSC111 Homework 3"
(→Problem #3: Secret Quote) |
(→Problem #3: Secret Quote) |
||
Line 139: | Line 139: | ||
python hw3c.py | python hw3c.py | ||
− | Your assignmnent: in this collection of words which were taken from an article of the New York Times of 2/10/10 (on Google Buzz) is hidden a quote that has nothing to do with the article. The quote is made of several words, the first one if which is the | + | Your assignmnent: in this collection of words which were taken from an article of the New York Times of 2/10/10 (on Google Buzz) is hidden a quote that has nothing to do with the article. The quote is made of several words, the first one if which is the 54th word in the article, and the others are at 54 word intervals. The first word of the quote is '''I''', the last word is '''Asimov''', the author of the quote. |
− | Modify the program '''hw3c.py''' so that it extracts and prints all the words that are | + | Modify the program '''hw3c.py''' so that it extracts and prints all the words that are 54 apart in the long list of words, reconstituting the original quote from Asimov. |
Submit our program as follows: | Submit our program as follows: |
Revision as of 17:32, 10 February 2010
This assignment is due at midnight on Thursday, 2/18/10.
Contents
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
Problem #3: Secret Quote
Python allows one to "attach" or include other python program in one's code. This is done by using the import directive. Look at the following program:
.
# hw3c.py
# 111c-xx
# this program imports a list of words from an outside Python
# program and lists the words on the screen
from hw3words import words # words is a list of words
for item in words:
print item
.
The part that starts from hw3words... says that the variable words which is defined in the python program hw3words should be made available to this program. words is a simple list of words. The for-loop simply takes each item of the list words and displays it on the screen.
Try it yourself, you need the file hw3words.pyc which I have prepared for this homework. Get a copy of it as follows:
getcopy hw3words.pyc
(The pyc extension is new for us. We haven't seen this yet. It indicates that the python program has been "compiled" into code that is easier for the computer to execute, but is not human-readable any longer.) Once you have a copy of hw3words.pyc in your directory, and your new hw3c.py program, run it as you would a normal program:
python hw3c.py
Your assignmnent: in this collection of words which were taken from an article of the New York Times of 2/10/10 (on Google Buzz) is hidden a quote that has nothing to do with the article. The quote is made of several words, the first one if which is the 54th word in the article, and the others are at 54 word intervals. The first word of the quote is I, the last word is Asimov, the author of the quote.
Modify the program hw3c.py so that it extracts and prints all the words that are 54 apart in the long list of words, reconstituting the original quote from Asimov.
Submit our program as follows:
submit hw3 hw3c.py
Problem #4: Optional and Extra Credit
Problem #4
This problem is an extension of the last problem. Your assigment for this one is to make your program print the quote that is extracted from the list words such that the quote itself is printed on one line, and the author's name (first and last name) on the next line.
We assume that the last two words of the quote will always represent the author's name. In other words, all the words of the quote should be printed on one line, except the last two words which should be printed on the next line.
Make sure that your program will work with a different list of words. The one you are using has about 713 words in it, but your program might be tested with a list of words of length, say, 285. In this case the quote will only be 5-word long. If your program is written correctly, it will output the first 3 words on one line, and the last two on the next line.
Store your program in a file called hw3c.py and submit it as follows:
submit hw3 hw3c.py