pandas: assign, query, sort_values

Model df.assign(total=price * qty).query("total > T").sort_values("total", ascending=False, kind="stable").

What to build

A program that adds a total column, filters it and sorts it.

Requirements

  • Compute total = price × qty for each item, keep the items whose total is greater than T, and sort them by total from largest to smallest, keeping tied items in input order.
  • Print item,total, then one line per kept item with the total to two decimals.

Input

Line 1: T. Line 2: n. Then n lines ITEM,PRICE,QTY.

Output

1 + K lines.

Constraints

1 ≤ n ≤ 1000; prices and quantities are 0 or more.

Example

Input:

10
4
pen,1.5,4
book,12,2
bag,30,1
cap,5,3

Output:

item,total
bag,30.00
book,24.00
cap,15.00

Why it works

Each step of the chain works on the result of the previous one, so filtering happens on the new column and sorting on the filtered rows, and a stable sort keeps ties predictable.

Examples

Example 1 · Input
10
4
pen,1.5,4
book,12,2
bag,30,1
cap,5,3
Expected output
item,total
bag,30.00
book,24.00
cap,15.00
Example 2 · Input
100
1
x,1,1
Expected output
item,total

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Compute total = price × qty for every row first.

Hint 2

sorted() is stable, so tied rows keep their input order when sorting by -total.

Solution

Show one correct solution (try it yourself first)
t = float(input())
n = int(input())
rows = []
for _ in range(n):
    item, price, qty = input().split(",")
    rows.append((item, float(price) * float(qty)))
kept = sorted((r for r in rows if r[1] > t), key=lambda r: -r[1])
print("item,total")
for item, total in kept:
    print(f"{item},{total:.2f}")

Lesson: Method chaining, apply and performance →