HardDictionariesNot started

Restock priority report

A small warehouse wants to order supplies before its next delivery. Each row has an item code, units currently on hand, expected units sold per day, and the number of days until replenishment. The required stock is daily demand times lead days. Print only items below that requirement, ordered by largest shortfall first; break ties by item code alphabetically.

Input

First line N. Next N lines: item_code current_units daily_demand lead_days.

Output

One line per understocked item: item_code, one space, shortfall. Print All stocked if none need replenishment.

Example 1

Input

4
tea 7 3 4
coffee 20 2 5
rice 1 2 3
flour 0 1 5

Output

flour 5
rice 5
tea 5

Coffee has enough stock. The other three tie on shortfall, so their item codes decide the order.

Constraints

  • 1 <= N <= 100
  • Item codes are unique lowercase letters, 1 to 20 characters
  • 0 <= current_units, daily_demand <= 1000
  • 1 <= lead_days <= 30

Hints

Hint 1 of 3

The reorder requirement is demand multiplied by lead days.

Hint 2 of 3

Only positive differences belong in the report; zero means enough stock.

Hint 3 of 3

Use a sort key with negative shortfall and the item code for ties.

Solution

Show a reference solution and explanation
 Python · reference solution
n = int(input())
urgent = []
for _ in range(n):
    code, current, demand, days = input().split()
    shortfall = int(demand) * int(days) - int(current)
    if shortfall > 0:
        urgent.append((code, shortfall))
urgent.sort(key=lambda item: (-item[1], item[0]))
if not urgent:
    print('All stocked')
else:
    for code, shortfall in urgent:
        print(code, shortfall)

Why it works

The calculation separates business rules from presentation: compute each positive shortage, store code and shortage together, then sort by (-shortage, code). Python compares tuple fields in order, so the negative number gives descending urgency while the code gives a stable alphabetical tie-break. An empty list needs an explicit status line.

Lesson for this exercise: Dictionaries in Python

Your program
n = int(input())
urgent = []
for _ in range(n):
    code, current, demand, days = input().split()
    # Calculate a positive shortfall and add it to urgent.
# Sort by descending shortfall, then ascending code.

Tests: 4 cases including the examples. Passing every test marks the exercise solved in this browser.

Ready for a challenge?

How this page was checked. Every program on it was run with CPython 3.11 at build time; runs in your browser on CPython 3.14 (Pyodide) by the publishing checks, and the output shown is what it printed. The Run buttons execute the same code on your device; nothing you type is sent anywhere.