Compsci 06, Spring 2011, Evil Hangman Stuff April 18

Name____________________   net-id _________       

Name____________________   net-id _________       

Name____________________   net-id _________       

  1. Which one of the lines below converts the list x = ['s','e','c','r','e','t'] into the string s = "secret". More than one may be correct.

    1. s = str(x)

    2. s = ''.join(x)

    3. s = [''+p for p in x]

  2. As in the previous problem, suppose x = ['s','e','c','r','e','t']. The list comprehension below creates a new list, which of the choices represents the result of the list comprehension?
     sub = ['e' if x[i] == 'e' else '_' for i in range(len(x))]
    

    1. sub = ['_', 'e', '_', '_', 'e', '_']

    2. sub = ['e', 'e', 'e,' 'e', 'e', 'e']

    3. sub = ['s', '_', 'c', 'r', '_', 't']

  3. Again, suppose x = ['s','e','c','r','e','t']. What is the value of the list comprehension below.
     sub = [ch if x[i] == 'e' else '_' for i,ch in enumerate(x)]
    

    1. sub = ['_', 'e', '_', '_', 'e', '_']

    2. sub = ['e', 'e', 'e,' 'e', 'e', 'e']

    3. sub = ['s', '_', 'c', 'r', '_', 't']

  4. Consider the dictionary di shown below: di = {'a' : ['d','e','f','g'], 'b' : ['f','g','h','i','j'], 'c' : ['a', 'b','c'], 'e' : ['p','q','r','s'] }

    Which of the following is the value of the list comprehension shown:

      pairs = sorted([(len(di[x]),x) for x in di])
    
    

    1. pairs = [(3,'c'), (4,'a'), (4,'e'), (5,'b')]

    2. pairs = [(5,'b'), (4,'a'), (4,'e'), (3,'c')]

    3. pairs = [(4,'a'), (5,'b'), (3,'c'), (4,'e')]

  5. Consider the dictionary di in the code above. The code below stores a list in variable best. best = [] for key in di: if len(di[key]) > len(best): best = di[key]

    What value is stored in best?

    1. []

    2. ['f','g','h','i','j']

    3. ['a','b','c']

  6. Which of the following stores the same value in best given the dictionary di? (more than one is possible)

    1. best = di[sorted([(len(di[x]),x) for x in di])[-1][1]]

    2. best = ['f', 'g','h','i', 'j']

    3. best = di[sorted([(x,len(di[x])) for x in di], key=itemgetter(1), reverse=True)[0][0]]