CSC111 Homework 3 Solutions

From dftwiki3
Revision as of 23:29, 24 February 2010 by Thiebaut (talk | contribs) (Problem #3)
Jump to: navigation, search

--D. Thiebaut 03:23, 25 February 2010 (UTC)


Problem 2

.

# hw3b.py                                                                                                                             
# 111c-as                                                                                                                                                       
# Xiao Huang                
# (Edited by D. Thiebaut)                                                                                     
# This program prompts the user for a number of 1-line poems to print,
#  and generates random poems made of random subjects, random adverbs,
# random verbs, random locations, and random times.
#   
from random import randrange

def main():
    print "Welcome to the Random poems of the Day!"

    # Ask for how many poems to generate                                                                            
    n = input("How many poems would you like me to generate for you?")

    # Create lists lists of subjects, adverbs, verbs and so on.                                                         
    subjects  = [ "Hello Kitty", "Professor 111", "Coco Chanel", "Penny" ]
    adverbs   = [ "mysteriously", "gently", "lazily", "violently", "doubtfully" ]
    verbs     = [ "decays", "daydreams", "eats nine burgers" ]
    locations = [ "in the bathroom", "on the moon ", "on the top of Himalayas","under the desk" ]
    times     = [ "during WWII", 
                      "before taking a walk in Central Park", 
                      "once a day","whenever possible",
                      "as soon as the end of the world comes" ]
    collection= [ subjects, adverbs, verbs, locations, times]

    # Generate n poems.
    for i in range (n):
        for item  in collection:
           # generate random index to pick random word in item
           count = len ( item ) 
           randomIndex = randrange (count)
           # print that item
           print  item[randomIndex],
        print

main()

.

Problem 3

.

# hw3c.py
# 111c-aw
# Andrea Spain
# This program takes a list of words and extracts every 54th word,
# starting at the 54th. It then prints them, forming a quote. The
# name of the author is printed on a second line.
 
from hw3words import words   #--- words is a list of words ---#

def main():
    #--- Creates empty list. ---#
    quote = [ ]

    #--- Starting at index 54 in words, appends items to the list using a step
    # of 54. It stops at the length of the list. ---#
    for i in range(54, len( words ), 54):
        quote.append( words[ i ] )

    #--- Prints the quote part itself, minus the last word. ---#
    for i in range( 0, len(quote)-3 ):    
        print quote[i],
    
    #--- Prints the last word of the quote. ---#
    print quote[-3] 
    
    #--- Prints the last two words (the name) onto a separate line. ---#
    for i in range( -2, 0):
        print quote[i],
    
main()

.