Top Seller of the Week

Python for AIMedium+10 XP+10

Top Seller of the Week

A bakery's sales file has one row per product. The manager wants to know which product did best, but the files don't always measure the same thing. Some weeks it's units, some weeks it's revenue.

Task: write top_by(path, text, label_column, value_column). It looks at the numbers in value_column and returns the value in label_column from the row with the highest number.

  • The values in value_column are numbers, possibly with a decimal point or a minus sign.
  • If two rows tie for highest, return the first one in the file.
  • If there are no data rows, return "".
  • Both columns always exist.

The starter code includes a small helper, write_text, and already calls it for you: it saves text into a file named path. Your code then opens that file and reads it. import csv is already at the top of the starter code.

Examples

path = 'bakery.csv'
text = 'product,units,revenue\nbun,40,20\ncake,12,36\nloaf,25,50\n'
label_column = 'product'
value_column = 'units'
returns 'bun'
path = 'bakery2.csv'
text = 'product,units,revenue\nbun,40,20\ncake,12,36\nloaf,25,50\n'
label_column = 'product'
value_column = 'revenue'
returns 'loaf'

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