Compsci 101, Fall 2012, October 29

By entering your name/net-id below you indicate you are in class on October 29 to answer these questions and that you have answered them. Your name should not appear unless you are in class when answering these questions.
Name____________________   net-id _________       Name____________________   net-id _________       

Name____________________   net-id _________       Name____________________   net-id _________       

    Dictionary Basics

    In a Python console the following appears illustrating some of the methods that work with dictionaries, in particular there's a variable d that stores a dictionary as shown. What the user types is shown in italics.
    >> d
    {'duke': 50, 'columbia': 30, 'stanford': 20}
    >>> d.keys()
    ['duke', 'columbia', 'stanford']
    >>> d.values()
    [50, 30, 20]
    >>> d.items()
    [('duke', 50), ('columbia', 30), ('stanford', 20)]
    >>> [x[1] for x in d.items()]
    [50, 30, 20]
    
    1. If the x[1] in the last line is replaced by x[0] what is printed?

      1. [20, 30, 50]

      2. ['duke', 'duke', 'duke']

      3. ['duke', 'columbia', 'stanford']

    2. After the user types d['duke'] = 80, what is printed by the expression d.values()?

      1. [50, 30, 20]

      2. [80, 30, 20]

      3. [50, 80, 30]

    3. The code below is executed next (after the value associated with 'duke' is changed to 80), what is printed?
      for name in d:
          d[name] += 10
      print d
      

      1. {'duke': 90, 'columbia': 40, 'stanford': 30}

      2. {'duke': 90, 'columbia': 30, 'stanford': 20}

      3. {'duke': 90, 'columbia': 40, 'stanford': 20}

      Jotto Stuff

    4. In the Jotto Assignment the function process_common_last is supposed to remove all words from the global variable _possiblewords that do not have common letters in common with the last word guessed by the computer player. A student suggests this code: for w in _possiblewords: if commonCount(w,_lastguess) != common: _possiblewords.remove(w) Which of the following is the best characterization of this code:

      1. It is not correct syntactically because either there is no remove method for lists or because the != operator is not defined.

      2. It expresses the right idea, but because a word is removed from a list during the loop/iteration over the list it is likely that the resulting list will contain words that should have been removed, but were not.

      3. The code works correctly.

    5. Suppose a list is created in process_common_last as follows: bw = [w for w in _possiblewords if commonCount(w,_lastguess) != common]

      Which one(s) of the following will change _possiblewords correctly (more than one is possible)?

      1. _possiblewords = list(set(_possiblewords) - set(bw))

      2. _possiblewords = [w for w in _possiblewords if not w in bw]

      3. _possiblewords = list(set(_possiblewords) & set(bw))