Novice vs competent:
Given the list a = [‘cheese’, ‘egg’, ‘spam’]. In which of the following statements is b a list containing all the items in a, sorted in order of increasing string length (in characters)?
- b = sorted(a, key=len)
- b = a.sort(by=’length’)
- b = sorted(a, cmp=’length’)
- b = a.sort(cmp=lambda x, y: cmp(len(x), len(y)))
Competent vs. expert:
The following dictionary is the output of a script that counts word frequency (occurrences) in a test: a = {‘cheese’: 30, ‘spam’: 4, ‘egg’: 500}. Which of the following approaches does not return a list of the words in order of frequency count?
- sort dictionary directly by key: sorted(a, key=a.get)
- sort dictionary keys with custom comparison function: sorted(a.keys(), cmp=lambda x,y: cmp(a[x], a[y]))
- sort dictionary items with a custom key: [item[0] for item in sorted(a.items(), key=lambda x: x[1])]
- using an iterator (operator library): from operator import itemgetter; b = sorted(a.iteritems(), key=itemgetter(1))