Compsci 6, Dictionary FUN March 21

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}

    4. In fingerPrint.py the function below returns the most frequently occurring word/count pair in data, a list of two-element lists, e.g., [['the',45],['cat',13],['dog',9]]

      Which is the best explanation for why this function works, i.e., why it returns the word/count pair that occurs most often?

      def max_list(data):
          return sorted([(elt[1],elt[0]) for elt in data])[-1]
      

      1. lists are sorted alphabetically by word, sorting puts the last word alphabetically at the end, and this word is returned.

      2. lists are sorted lexicographically, elt[1] represents the count, and the last tuple in the list is the largest number of occurrences.

      3. the function returns a tuple that's the most-frequently occurring word because the [-1] ensures that the last element is returned.