All challenges

Saturday, 14 November 2026

Past

Three rungs, one idea. Solving rung 1 counts. Climb as far as you can.

Shopping Basket

dictionaries + looking up prices

Rung 1Daily goal5 points

Add up the basket

Write a function called basket_total

You get a dictionary of prices in pence, like {"apple": 30, "pear": 45}, and a list of the items in a basket. Return the total price of everything in the basket, counting an item again each time it appears. Every item in the basket is in the price dictionary, and an empty basket costs 0.

Examples

InputOutput
{"apple": 30, "pear": 45}, ["apple", "pear", "apple"]105
{"apple": 30}, []0
{"apple": 30}, ["apple"]30
{"gum": 5, "pop": 120}, ["pop", "pop"]240

There are extra hidden tests, so make it work in general, not just for these.

Rung 2Bonus7 points

Some are not on sale

Write a function called known_total

Now some items in the basket might not be in the price dictionary. Those items cannot be bought, so leave them out and add up only the ones that have a price. If nothing in the basket has a price, or the basket is empty, return 0.

Examples

InputOutput
{"apple": 30, "pear": 45}, ["apple", "plum", "pear"]75
{"apple": 30}, ["plum"]0
{}, ["apple"]0
{"apple": 30}, ["apple", "apple"]60

There are extra hidden tests, so make it work in general, not just for these.

Rung 3Bonus10 points

The bill for each item

Write a function called spend_by_item

Return a dictionary showing how much was spent on each item: the item's name points to its price times how many of it are in the basket. So ["apple", "pear", "apple"] with apples at 30 and pears at 45 gives {"apple": 60, "pear": 45}. Leave out any item that is not in the price dictionary. If nothing can be bought, return an empty dictionary.

Examples

InputOutput
{"apple": 30, "pear": 45}, ["apple", "pear", "apple"]{"apple": 60, "pear": 45}
{"apple": 30}, []{}
{"apple": 30}, ["plum"]{}
{"gum": 5}, ["gum"]{"gum": 5}

There are extra hidden tests, so make it work in general, not just for these.

Finish Rung 1 and today counts. Rungs 2 and 3 are there if you want to push further.