Problem 190303 · easy · Level 01 Prerequisites & Setup

Poll Results, Empty Rows Included

relative frequency · percentages · dictionaries · frequency tables

A town asked residents which new facility they would like most. The ballot listed a fixed set of options, and responses holds one answer per resident. For the newsletter the council wants the percentage of residents who chose each option, in the order of the ballot, and an option nobody picked must still appear (with 0.0): a missing row is easy to overlook.

Write poll_percentages(responses, options) that returns a list of floats, one per option, where each value is 100 * (number of residents who chose it) / (number of responses). If there are no responses at all, every percentage is 0.0. Do not round.

Examples

Input:  responses = ["pool", "library", "pool", "skate park", "pool"],
        options = ["library", "pool", "cinema", "skate park"]
Output: [20.0, 60.0, 0.0, 20.0]

Input:  responses = ["cinema", "pool", "library"], options = ["pool", "library", "cinema"]
Output: [33.333333333333336, 33.333333333333336, 33.333333333333336]
Explanation: the unrounded percentages add up to 100; rounded to whole numbers they would give 99.

Constraints

  • 1 <= len(options) <= 100, all options different
  • 0 <= len(responses) <= 10**5, and every response is one of the options
  • percentages are compared with a small tolerance

Goals

  • Turn counts into percentages of the whole
  • Report every option, including those nobody chose
  • Avoid dividing by zero when there is no data
Starting Python…