Inventory Total Cost

Inventory Total Cost

Medium Python OOP Basics 20 views
Explanation Complexity

Problem Statement

Read n items each with name, price, qty. 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.

Example

3
pen 10 2
book 50 1
eraser 5 3
85

Constraints

1

Input / Output Format

Input Format
First line n. Next n lines: name price qty.
Output Format
One integer total cost.
Constraints
1

Examples

Input:
3 pen 10 2 book 50 1 eraser 5 3
Output:
85

Example Solution (Public)

Python
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))

Official Solution Code

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))
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.