The Rare Ones

Data with PythonEasy+10 XP+10

The Rare Ones

A pet shop survey asks customers which pet they own. A few answers, like axolotl, turn up only once or twice. Before the answers go into a model, the shop wants to lump the rare ones together, so first it needs to know which ones are rare.

Task: write rare_categories(answers, min_share).

  • answers is a list of strings. A missing answer is None.
  • Missing answers are left out completely. Shares are worked out from the answers that are there.
  • An answer is rare when its share of those answers is less than min_share (a number between 0 and 1). A share exactly equal to min_share is not rare.
  • Return a list of the rare answers, each one once, sorted alphabetically. If nothing is rare, return an empty list.

Examples

answers = ['dog', 'cat', 'dog', 'fish', 'cat', 'dog', 'axolotl', 'cat', 'dog', 'cat']
min_share = 0.15
returns ['axolotl', 'fish']
answers = ['dog', None, 'cat', 'dog', 'rabbit', None, 'dog', 'cat']
min_share = 0.2
returns ['rabbit']

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run