Total Amount and Bad Lines
Python
Hard
2 views
Problem Description
Read n lines, each should be 'name amount'. amount must be integer. Sum valid amounts and count bad lines. Output total and badCount.
Input Format
First line n. Next n lines.
Output Format
One line: total badCount.
Sample Test Case
Input:
5
tea 10
coffee x
milk 20
badline
sugar -5
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if not lines: sys.exit(0)
try:
n=int(lines[0].strip())
except Exception:
sys.stdout.write('0 0')
raise SystemExit
bad=0
sm=0
for i in range(1,1+n):
parts=(lines[i] if i<len(lines) else '').split()
if len(parts)!=2:
bad+=1
continue
try:
sm+=int(parts[1])
except Exception:
bad+=1
sys.stdout.write(f'{sm} {bad}')
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!