Inventory Total Cost
Programming Interview
Medium
7 views
Problem Description
We have {x}. Create Item class with total() method. Output grand total cost.
Input Format
First line n. Next n lines: name price qty.
Output Format
One integer total cost.
Sample Test Case
Input:
3
pen 10 2
book 50 1
eraser 5 3
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if not lines: sys.exit(0)
n=int(lines[0].strip())
class Item:
def __init__(self,name,price,qty):
self.name=name
self.price=price
self.qty=qty
def total(self):
return self.price*self.qty
sm=0
for i in range(1,1+n):
parts=(lines[i] if i<len(lines) else '').split()
if len(parts)<3: continue
it=Item(parts[0],int(parts[1]),int(parts[2]))
sm+=it.total()
sys.stdout.write(str(sm))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!