Embercode
✕
SortingQuick quiz

Multi-key and frequency order

8 questions. Covers Custom order by frequency in Python.

Questions 1 to 5 of 8

  1. 1. Which key sorts students by grade from high to low, and then by name from A to Z?
  2. 2. The key lambda v: counts[v] prints [6, 2, 2, 6, 4, 4, 4, 4] for the input [4, 6, 2, 2, 6, 4, 4, 4]. Why are the two 6s not kept together with the 2s?
  3. 3. What does this program print? ``python from typing import NamedTuple class Student(NamedTuple): name: str grade: int students = [Student("Mia", 75), Student("Kai", 90), Student("Lee", 75), Student("Noor", 90), Student("Ola", 60)] print(" ".join(s.name for s in sorted(students, key=lambda s: (-s.grade, s.name)))) ``
  4. 4. What does this program print? ``python from collections import Counter values = [5, 3, 5, 9, 3, 5] counts = Counter(values) print(sorted(values, key=lambda v: (-counts[v], v))) ``
  5. 5. What does this program print? ``python from collections import Counter text = "level" counts = Counter(text) print(sorted(text, key=lambda c: (-counts[c], c))) ``

5 question(s) left on this page.