FunctionRacerProblemsRace

Stock After the Day

EasyHash mapsPython, JavaScript, TypeScript, Java, C++

events lists what happened in a shop during one day, in order. Each event is a string "in NAME N" or "out NAME N", where NAME is a product name and N a positive whole number. The shop starts with nothing.

in adds N units of the product. out removes N units, but never more than the shop has: if only M < N units are left, those M units go and the rest of the order is dropped.

Return a dictionary mapping each product that has at least one unit left at the end to its number of units. Products at zero are left out. No events gives an empty dictionary.

Example 1

events ["in apple 5","out apple 2","in pear 3","out pear 5"]
returns {"apple":3}

Apples: 5 in, 2 out, 3 left. Pears: 3 in, then an order for 5 takes the 3 that exist, 0 left, so pear is not in the answer.

Example 2

events ["in milk 2","out milk 2"]
returns {}

The two milk units come in and go out again.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Hash maps problems