Ledger with GET
Python
Medium
2 views
Problem Description
You have a dictionary of balances. Commands: ADD name x, SUB name x, GET name. Missing name starts at 0. For GET, output current balance.
Input Format
First line q. Next q lines commands.
Output Format
Outputs for GET commands.
Sample Test Case
Input:
6
ADD a 10
ADD b 5
SUB a 3
GET a
GET c
GET b
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if not lines: sys.exit(0)
q=int(lines[0].strip())
mp={}
out=[]
for i in range(1,1+q):
parts=(lines[i] if i<len(lines) else '').split()
if not parts: continue
cmd=parts[0]
if cmd=='GET':
name=parts[1]
out.append(str(mp.get(name,0)))
else:
name=parts[1]
x=int(parts[2])
if cmd=='ADD':
mp[name]=mp.get(name,0)+x
else:
mp[name]=mp.get(name,0)-x
sys.stdout.write('\
'.join(out))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!