Compsci 06, Spring 2012, Hangman April 17

Name____________________   net-id _________     Name____________________   net-id _________       

Name____________________   net-id _________     Name____________________   net-id _________       

These questions are about the Clever Hangman assignment.

Suppose that the game is just starting and the entire dictionary of words that the computer can choose from in guessing a five-letter word is:

["apple", "upper", "happy", "puppy", "hippo", "creep", "pumps", "bicep",
 "vocal", "poppy", "space", "aphid", "opera", "alpha", "bagel",
 "maple", "viper", "grape", "trips", "paper", "ghost", "peeps"
]
 
  1. The user has guessed the letter 'p' there are 22 words to be considered. The word "happy", has as it's pattern "_ _ p p _" for the purposes of categorizing words. Which list below is the complete list of words with the same pattern with respect to 'p'.

    1. ['happy', 'puppy', 'hippo', 'poppy']

    2. ['happy', 'hippo']

    3. ['happy', 'hippo', 'apple', 'upper']

  2. The user has guessed the letter 'p' and there are 22 words to be considered. Among the patterns for 'p' are "_ _ _ _ _", "p _ _ p _" and "_ p _ _ _", If you write down all the patterns with respect to "p", including the "_ _ p p _" from the previous problem, how many different patterns are there?

    1. 8

    2. 10

    3. 12

  3. The function get_pattern is designed to produce a list/pattern such as those described above, e.g., so that the code below stores ['_','p','_','_','_] in variable pattern.

      temp = ['_']*5
      word = "opera"
      pattern = get_pattern(temp,word,'p')
    
    def get_pattern(template,word,char): nt = list(template) for i in range(len(word)): ch = word[i] if ch == char: nt[i] = ch return nt

    What is the purpose of using the code nt = list(template) above?

    1. to ensure that template and word have the same length

    2. to create a list from the string parameter template

    3. to create a copy of template so that the parameter isn't changed.

  4. The value returned by get_pattern is a list. Which code below creates a string from the list, e.g., "_p___" from ['_','p','_','_','_']

    1. ''.join(pattern)

    2. ','.join(pattern)

    3. ' '.join(pattern)

  5. A string like "_ p p _ _" can be a key in a dictionary, mapped to all the possible words that have that pattern, e.g., some entries from the list above include:
    {'p_p__': ['paper'], 
     '_____': ['vocal', 'bagel', 'ghost'],
    
    
    Once the dictionary is created, which code below below finds the key (string) with the value (list) that has the most elements. The dictionary is d. More than one can be correct.

    1. sorted(d.items(),key=operator.itemgetter(1), reverse=True)[0]

    2. sorted(d.items(),key=operator.itemgetter(1))[-1]

    3. sorted([(d[key],key) for key in d],reverse=True)[0][1]