CSC111 Homework 5 Solution Programs 2014
--D. Thiebaut (talk) 16:02, 12 March 2014 (EDT)
Program #1
# hw5a.py
# Katerina Popova ( af )
# 02/03/2014
# A program that creates random one-line poems.
# A poem will contain a subject, an adverb, a verb, a location, and a time.
# If we pick a random word from each list and
# "glue" all the words together, we end up with a one-line poem.
# The program declares a different series of lists
# (lists containing different number of words),
# and asks the user how many one-line poems she would like to see.
# The program then prints that many different poems.
# --------------------------------------------------------
#importing the random choice function
from random import choice
from random import seed
seed()
# creates a list of subjects, adverbs, verbs, location, and times
subjects = [ "My brother", "Dominique", \
"My professor", "The squirrel", "Katerina" ]
adverbs = [ "restlessly", "usually", \
"amazingly", "endlessly", "often" ]
verbs = [ "does homework", "takes a nap", "programs" ]
locations = [ "in Norhtampton", "in the sea", "in lab", "in Bulgaria" ]
times = [ "before class", "after dinner", "during class",
"at night", "all the time", "during finals", "during senior week" ]
# a welcome-message for the user (user-friedly program)
print ( "\n\nWelcome to the Random Poems of the Day!\n" )
# a prompt asking the user for
# the number of poems to be generated
noPoems = int( input( "How many poems would you like me to generate for you? " ) )
# prompting the user to enter a valid value
# if the value is 0 or a negative number the first time
while noPoems <= 0:
noPoems = int( input( "Please, enter a value greater than 0: " ))
s = "s"
if noPoems == 1: s = ""
print( "Here are your %d poem%s:" % ( noPoems, s ) )
# generating a random word/expression from each list
# for every iteration -> choice function should be inside the loop
for i in range(0, noPoems ):
subject = choice ( subjects )
adverb = choice ( adverbs )
verb = choice ( verbs )
location = choice ( locations )
time = choice ( times )
print (subject, adverb, verb, location, time)
Program 2
# hw5b.py
# Erika Earley (ar)
# (slightly edited by D. Thiebaut)
# 3/2/2014
#
# Problem 2 : play with an array and several variables created in a separate file
# called hw5data.py by clearing, copying, and replicating values in the array
# The array contains several "words" of equal length separated by gaps of 0s.
# The beginning index of each word is defined by three variables retrieved from
# hw5data.py: firstWordIndex, secondWordIndex, and thirdWordIndex.
#
# These "words" are supposed to represent sound samples of words said by a
# person in a sound file.
#
# ---- import data from separate file called hw5data.py
from hw5data import WORDLENGTH
from hw5data import GAPLENGTH
from hw5data import array
from hw5data import noSamples
from hw5data import firstWordIndex
from hw5data import secondWordIndex
from hw5data import thirdWordIndex
# ----- Question 1: clear the first and last word of the array
print("Original Array = ", array)
# ----- clear the first word (replace all values in first word with zero)
for i in range( 0, secondWordIndex ):
array[i] = 0
# ----- clear the third word (replace all values in third word with zero)
for i in range( thirdWordIndex, noSamples):
array[i] = 0
# ----- store new array as a new variable to stress that the array
# ----- is cleared except for word 2 values
arrayWord2Only = array
print( "Clear all except Word 2 = ", arrayWord2Only )
# ----- Question 2: replicate the second word and put it at the front
# ----- and back of the array
# ----- find the length for the word and the gap combined
FULLWORD = WORDLENGTH + GAPLENGTH
# ----- take each value of the second word and place it in the appropriate
# ----- location directly after the second word so as to spell out an exact
# ----- copy at the end of the array
for i in range ( secondWordIndex, thirdWordIndex):
j = i + FULLWORD
value = array[i]
array[j] = value
# ----- take each value of the second word and place it in the appropriate
# ----- location directly before the second word so as to spell out an exact
# ----- copy at the beginning of the array
for i in range ( secondWordIndex, thirdWordIndex):
j = i - FULLWORD
value = array[i]
array[j] = value
# ----- store new array as a new variable to stress that the array
# ----- has word 2 replicated at beginning and end of array
arrayWord2Rep = array
print("Word 2 replicated = ", arrayWord2Rep, "\n\n" )