Editor

The Priciest Item

#524 · Loops · Beginner · Review the AI's code · 25 XP · about 3 min

Someone asked an AI assistant: "Write a function that finds the most expensive item in a list of (name, price) pairs. If two items cost the same, return the first one." This is its answer. It looks right, but it has a bug.

priciest(items) should return the name of the item with the highest price. If several items share the highest price, it returns the one that appears first in the list. For an empty list it returns None.

Examples

priciest([('pen', 2.5), ('book', 12.0), ('mug', 8.0)])  # returns 'book'
priciest([('tea', 4.0), ('jam', 4.0)])                  # returns 'tea'
priciest([])                                            # returns None

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.