# Quiz #4 Solutions # November 6, 2012 # The code blocks were worth 2 points apiece. # 1a The adverbify function just puts an ly at the end of a word, giving # Come quickly this is oddly. # 1b The function subtracts mod from number until number is no longer larger # than mod. So reduce(12,4) brings 12 down to 4, while reduce(5,12) doesn't # subtract anything from 5. The function returns 4 in the first case and # 5 in the second case, giving a sum of # 9 # 1c "Crabby" is removed, then "Grumpy" is appended to the end of the list. # At this point the third dwarf in the list (i.e. position 2) is # Bashful # 1d We delete "howdy" and then alphabetize and print out the words in order, # aloha hi hola # 1e We repeatedly insert the letters 't', 'i', 'r', 'e' into the list at # position 1 (i.e. the second spot). Each subsequent insertion pushes # the earlier ones to the right, resulting in the list # ['m','e','r','i','t','o','u','s'] # 2 (3 points) The corrected code appears below. Notice the use of a space " " # instead of an empty string "" in the fourth line. def removespace(phrase): condensed = "" for letter in phrase: if letter != " ": condensed = condensed + letter return condensed phrase = raw_input("Type some words: ") print removespace(phrase) # 3 (7 points) There are two fairly different ways to set up this program. # An example of each is shown below. shortwords = [] for j in range(0,len(wordbank)): if len(wordbank[j]) >= 6: print wordbank[j] else: shortwords.append(wordbank[j]) del wordbank[j] shortwords.sort() # --- OR --- shortwords = [] for word in wordbank: if len(word) >= 6: print word else: shortwords.append(word) wordbank.remove(word) shortwords.sort()