Compsci 101, Fall 2012, November 5

By entering your name/net-id below you indicate you are in class on November 5 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 _________       

Sorting Stuff

In the SortByFreq APT a list of words is returned in-order by number of occurrences (first) and alphabetically (to break ties). For example, one of the examples is:
data = ["apple", "pear", "cherry", "apple", "pear", "apple", "banana"]

Returns: ["apple", "pear", "banana", "cherry" ]

Where banana appers before cherry in the list because it's alphabetically first (break the tie of one occurrence each).

  1. The first step in solving this APT is to count word occurrences. Which of the line(s) shown below can be used in the loop to update the dictionary dc (more than one line can be correct)?
      def sort(data):
          dc = {}
          for w in data:
              # missing line here
    

    1. dc[w] = dc[w] + 1

    2. dc[w] = dc.get(w,0) + 1

    3. dc[w] = dc[w]+1 if w in dc else 1

  2. If dc is a dictionary storing (word,freq) tuples from the parameter data which of the line(s) below creates a list of tuples sorted alphabetically (more than one is possible)?

    1. alph = sorted(d.items(), key=operator.itemgetter(0))

    2. alph = sorted(d.items(), key=operator.itemgetter(1))

    3. alph = sorted(d.items())

  3. if alph is a list of (word,freq) tuples sorted alphabetically by word, which line creates a list that will work for the APT, i.e., sorted by frequency, with highest frequency first, and ties broken by lexicographical word order (more than one is possible)?

    1. fr = sorted(alph, key=operator.itemgetter(1),reverse=True)

    2. fr = sorted(alph, key=operator.itemgetter(1))

    3. fr = sorted(alph, reverse=True)

  4. In the MedalTable APT a sorted list is returned where the items being sorted are listed in order first by number of gold-medals won, then ties broken by number of silver-medals, then ties broken by number of bronze-medals, then ties broken lexicographically by country abbreviation which is unique. You'll use a several-pass sort, sorting data by different criteria, e.g., number of gold-medals, country abbreviation, etc. Which sort should be done first?

    1. Number of gold-medals since that's the criteria that governs the overall-order, e.g., highest number of gold-medals first, fewest number of gold-medals last.

    2. Country abbreviation since that is the final tie breaker so sorting by that criteria first ensures any subcategory of future sorting will be in-order by country.

    3. Combined number of medals, e.g., gold+silver+bronze since that will ensure that medal-count ranking is done properly.

  5. In the SerialNumber APT you can create a 3-tuple, sort it in several passes, then use that to create a return value. Which of the following is a good description of the three tuple (where val is one element from the paramater numbers).

    1. (len(val),digsum(val),val)

    2. (len(val),val.split("0-9"),val)

    3. (len(val),sum(val),val)