Bars as Percentages

Data with PythonEasy+10 XP+10

Bars as Percentages

A survey asked people for their favourite fruit. The chart should show one bar per fruit on the menu, as a percentage of all answers, in the menu's order.

Task: write percent_bars(answers, menu). Return one number per item in menu, in the same order: the share of answers that picked it, as a percentage rounded to 1 decimal place.

  • A menu item nobody picked gets 0.0.
  • Every answer is on the menu.
  • If there are no answers at all, every bar is 0.0.

Examples

answers = ["apple", "pear", "apple", "kiwi"]
menu = ["apple", "kiwi", "pear"]
returns [50.0, 25.0, 25.0]
answers = ["fig", "fig", "fig"]
menu = ["date", "fig"]
returns [0.0, 100.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