After-sales shortfalls
Each product row gives SKU, opening stock, units sold and required minimum. Calculate remaining stock as opening minus sold. For products below minimum, print SKU and the units needed to return to minimum, sorted by SKU. Print NONE if every product is adequately stocked.
Input
First line N; then N lines SKU opening sold minimum, with unique SKUs and 0 <= sold <= opening.
Output
Sorted SKU shortfall lines, or NONE.
Example 1
Input
3 tea 8 5 5 rice 10 2 4 milk 4 4 2
Output
milk 2 tea 2
Milk has zero remaining against minimum two, and tea has three against minimum five; rice remains above its minimum.
Constraints
- 1 <= N <= 200 unique SKUs.
- For each row, opening, sold and minimum are non-negative integers with sold <= opening.
Hints
Hint 1 of 2
Remaining stock is opening minus sold.
Hint 2 of 2
A shortfall exists only when minimum minus remaining is positive.
Solution
Show a reference solution and explanation
n = int(input())
short = {}
for _ in range(n):
sku, opening, sold, minimum = input().split()
remaining = int(opening) - int(sold)
needed = int(minimum) - remaining
if needed > 0:
short[sku] = needed
if not short:
print('NONE')
else:
for sku in sorted(short):
print(sku, short[sku])
Why it works
Compute the post-sale balance before comparing with the minimum; comparing opening stock would miss recent sales. Store only positive deficits and sort SKUs for a reproducible purchase list. Equality with the minimum is adequate, not a shortfall.
Lesson for this exercise: Dictionaries in Python