CSC111 Homework 3 Solution Programs 2011

From dftwiki3
Revision as of 06:41, 13 October 2011 by Thiebaut (talk | contribs) (Created page with "--~~~~ ---- ==Interesting/surprising/strange random poems gathered from the class== * Kevin Devine usually listens to Manchester Orchestra at home every Sunday. * Man silently...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut 07:41, 13 October 2011 (EDT)


Interesting/surprising/strange random poems gathered from the class

  • Kevin Devine usually listens to Manchester Orchestra at home every Sunday.
  • Man silently dreams 'neath the old pear tree at twilight
  • Our neighbor invariably does cartwheels at the store every day
  • Goat farmer slowly likes frolicking in the Sahara as often as possible
  • My math professor usually sings at home every Sunday.
  • Professor Thiebaut eats sweet oranges deliciously with bears on early mornings
  • Chuck Norris graciously bosses me around to infinity and beyond at night.
  • Jackie Chan savagely munches on cookies on TV during school
  • Flying Spaghetti Monster viscously lectures on the roof before class.

Peoms

# hw3a.py
# 111a-bg
# Sydney Ness
# This is a program that creates lists of subjects, adverbs, verbs, locations and times
# and uses random list elements to create a number (inputted by user) of random poems
#
# Example of user interaction:
# Welcome to Random Poems of the Day!
# How many poems would you like me to generate for you? 2
#
# Here are your 2 poems:
#
# Kevin Devine usually sings in Talbot House last at night.
# My math professor never loves pizza underwater all the time.


from random import randrange

def main():

   print( "Welcome to the Random Poems of the Day!" )

   # ask user how many poems to print
   numberPoems = eval(input( "How many poems would you like me to generate for you? "))
   print()

   # create lists of subjects, adverbs, verbs, locations, times
   subjects = [ "Kevin Devine", "The record player", "iTunes", "My math professor", "Amy Poehler" ]
   adverbs = [ "usually", "sweetly", "quietly", "never" ]
   verbs = [ "sings", "eats pizza", "loves pizza", "listens to Manchester Orchestra" ]
   locations = [ "at home", "in Talbot House", "underwater" ]
   times = [ "all the time", "late at night", "at sunrise and sunset", "every Sunday" ]

   # create a collection of the lists that loops will reference
   words = [ subjects, adverbs, verbs, locations, times ]

   print( "Here are your", numberPoems, "poems:")
   print()

   # outer loop ensures that desired number of poems is printed
   for j in range( numberPoems ):
      for i in words:
         # pick a random number from the length of the list
         randomIndex = randrange( len(i) )
         #print word in list that corresponds to that number, followed by a space
         print( i[ randomIndex ], end=" " )
      # deletes the last space that was printed and inserts a period
      # goes to new line for next poem
      print( "\b" + ".", end = "\n")

main()

Forms


# hw3b.py
# 111a-av Victoria Wu
# Edited by D. Thiebaut
#
# This program prompts the user for the dimensions 
# of a form, and generates a form with the proper 
# headings for candidate names, an extra label, and 
# space for comments. Note: If the characters in the
# extra label exceed 13, then the form will not be
# aligned properly. Also, if the input form width
# is less than the number of characters in the 
# candidates names, the program will not work.
#
## Another note: An extra line is added before the 
## second for loop after I tested the program against
## hw3test1.txt, otherwise the first line of the box
## was printed on the same line as the Candidates.

def main():
    # get data from user
    formWidth = eval(input("What is the width of the form? "))
    formLabel = input("What is the label of the box under the date? ")
    print()
    noCandidates = eval(input("How many candidates do you have? "))

    # get the candidate names and save in a list
    candidates = []
    for i in range(1, noCandidates+1):
        candidates.append( input( "Candidate " + str(i) + ": "))

    # output forms for each name
    print()
    for name in candidates:
        # top line
        print("+" + "-"*(formWidth - 17) + "+" + "-"*(14) + "+") #print extra +
        # name and date
        print("|" + name + " "*(formWidth - len(name) - 17) + "| date:        |")  
        print("|" + " "*(formWidth - 17) + "+" + "-"*(14) + "+") 
        # label 
        print("|" + " "*(formWidth - 17) + "| " + formLabel + " "*(13 - len(formLabel)) + "|") 
        print("|" + " "*(formWidth - 17) + "+" + "-"*(14) + "+") 
        # top box
        print("|" + " "*(formWidth - 2) + "|")
        print("+" + "-"*(formWidth - 2) + "+")
        # bottom box
        print("|" + " Comments:" + " "*(formWidth - 12) + "|")   
        for i in range( 3 ):
            print("|" + " "*(formWidth - 2) + "|")
        print("+" + "-"*(formWidth - 2) + "+")
        # extra blank lines
        print( "\n" )


main()



Centered Poems


# hw3c.py
# 111a-ah
# Kate Aloisio
# Edited by D. Thiebaut
#
# This program creates a number (specified by user) of random poems. Then it
# finds the longest poem and centers the other poems relative to the longest.
#
# Limitation: Needs at least one poem otherwise the sum() function crashes

from random import randrange

def main():

    # create lists of subjects, adverbs, verbs, locations, times
    subjects = ["The bank ", "Newton ", "The cloud ", "Agent green ", "The brain "]
    adverbs = ["practically ", "simply ", "foolishly ", "unnecessarily ", "suitably "]
    verbs = ["correlates ", "responds ", "analyzes ", "computes ", "conspires "]
    locations = ["at a candlelit table ", "under the ivy vine ", "in the kitchen "]
    times = ["everyday ", "at twilight ", "at dawn "]
    
    # greeting
    print("Welcome to the Random Poems of the Day!")
    noPoems = eval(input("How many poems would you like me to generate for you? "))

    # length of each list
    subjectCount = len(subjects) 
    adverbCount = len(adverbs)
    verbCount = len(verbs)
    locationCount = len(locations)
    timeCount = len(times)

    print("\nHere are your",noPoems,"poems: \n" )
    
    # loop for number of poems requested
    poems=[]
    poemsLen=[]
    for i in range(noPoems):

        # pick a random number for each list
        randomIndexS = randrange(subjectCount)
        randomIndexA = randrange(adverbCount)
        randomIndexV = randrange(verbCount)
        randomIndexL = randrange(locationCount)
        randomIndexT = randrange(timeCount)

        # create poem and calculate length of poem
        poem =  Subjects[randomIndexS] + adverbs[randomIndexA] + verbs[randomIndexV] \
                   + locations[randomIndexL] + times[randomIndexT]
        poemLength = len(poem)
        
        # add poem to list of poems
        poems.append(poem)
        # add length of poem to list of lengths
        poemsLen.append(poemLength)
        
    # finding the poem with the maximum length
    mPoem=max(poemsLen)
    
    # print poems with indents
    for poem in poems:
        pLen = len(poem)
        print(" "*((mPoem-pLen)//2)+poem)

    print()


main()



Money





Extra Credit