Compsci 6, Sorting FUN March 28

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 is possible)?
      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=itemgetter(0))

    2. alph = sorted(d.items(), key=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=itemgetter(1),reverse=True)

    2. fr = sorted(alph, key=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.


    Jotto Stuff

  5. 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 eitehr there 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.

  6. 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))