#K5061. Vending Machine Simulation
Vending Machine Simulation
Vending Machine Simulation
This problem simulates a simple vending machine. The vending machine has 5 slots, with initial inventory given by \(\textbf{items} = [10, 5, 3, 8, 6]\). Each slot is identified by an index from 0 to 4.
You are given a series of operations. Each operation is specified by a slot number and an action. There are two possible actions:
- dispense: Dispense an item from the specified slot. If the slot is valid and there is at least one item available, the machine dispenses an item (i.e. decrements the count) and returns
True
. If the slot is invalid or the slot is empty, returnFalse
. - count: Return the current number of items in the specified slot. If the slot number is invalid, return \(-1\).
Note: The operations are processed sequentially; thus, the state of the vending machine (i.e. the inventory) changes over the sequence of operations.
inputFormat
The input is read from standard input (stdin). The first line contains an integer \(T\) representing the number of operations. Each of the following \(T\) lines contains two values separated by a space:
- An integer \(slot\) (which can be negative or up to any integer) representing the slot number.
- A string \(action\) which is either "dispense" or "count".
outputFormat
For each operation, output the result on a new line. For a 'dispense' operation, print "True" if an item is dispensed, otherwise print "False". For a 'count' operation, print the current number of items in that slot (or \(-1\) if the slot number is invalid).
## sample5
1 dispense
1 count
0 count
3 dispense
3 count
True
4
10
True
7
</p>