Counting the Blanks Too

Data with PythonMedium+10 XP+10

Counting the Blanks Too

A school survey asks pupils for their favourite lunch. Some pupils skip the question, and a skipped answer is stored as None.

The head wants a share for every answer, including the skipped ones. That way the shares add up to the whole year group, and nobody quietly disappears from the count.

Task: write answer_shares(answers). answers is a list of strings, with None for a skipped question.

  • Return a list of [answer, percent] pairs, one for each different answer.
  • Skipped answers are counted together under the name "no answer". Only include that pair if at least one answer was skipped. No real answer is called "no answer".
  • percent is the share of all entries in answers, skipped ones included, as a percentage rounded to 1 decimal place.
  • Order the pairs by count, biggest first. If two pairs have the same count, put them in alphabetical order of their name ("no answer" sorts like any other name).
  • If answers is empty, return an empty list.

Examples

answers = ["pasta", "curry", "pasta", None, "soup", "pasta", "curry", None, "pasta", "curry"]
returns [['pasta', 40.0], ['curry', 30.0], ['no answer', 20.0], ['soup', 10.0]]
answers = ["yes", "no", "yes", "yes"]
returns [['yes', 75.0], ['no', 25.0]]

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