#C8203. Inventory Update Simulation
Inventory Update Simulation
Inventory Update Simulation
You are given an initial inventory for a fruit store and a list of daily transactions. Each transaction can either be a sale (which subtracts from the current stock) or a restock (which adds to the current stock). The inventory is represented as a list of fruit names with their quantities, and the transactions are given in order. Note that if a transaction involves a fruit that is not in the initial inventory, it should be ignored.
You are required to update the inventory by applying each transaction in the given order. The final output must list the updated quantity for each fruit in the same order as the input inventory.
Formally, let \(Q(f)\) be the quantity for fruit f in the inventory. For each transaction, if the transaction is of type sale then perform:
[ Q(f) = Q(f) - \text{quantity} ]
and if it is of type restock then perform:
[ Q(f) = Q(f) + \text{quantity} ]
Your task is to write a program that reads the input from stdin
and outputs the final inventory counts to stdout
.
inputFormat
The input is provided via stdin
and has the following format:
- The first line contains an integer m, the number of fruits in the initial inventory.
- The next m lines each contain a fruit name (a string without spaces) and its quantity (an integer), separated by a space.
- The following line contains an integer n, the number of transactions.
- The next n lines each contain a transaction in the format: type (either
sale
orrestock
), fruit name, and quantity, separated by spaces.
If n is 0, there will be no transaction lines.
outputFormat
Output the updated inventory to stdout
with m lines. Each line should contain a fruit name and its updated quantity, separated by a space, in the same order as they appear in the initial inventory.
3
apple 50
banana 30
grape 20
4
sale apple 10
restock banana 20
sale grape 5
sale banana 10
apple 40
banana 40
grape 15
</p>