Two Variables Calculator
Python
Medium
2 views
Problem Description
You have two variables a and b starting at 0. You get q commands: SETA x, SETB x, ADDA x, ADDB x, SWAP. Output final a and b.
Input Format
First line q. Next q lines commands.
Output Format
One line: a b.
Sample Test Case
Input:
5
SETA 5
SETB 2
ADDA 3
SWAP
ADDB 10
Official Solution
import sys
lines=sys.stdin.read().strip().splitlines()
if not lines or not lines[0].strip():
sys.exit(0)
q=int(lines[0].strip())
a=0
b=0
for i in range(q):
parts=(lines[i+1] if i+1<len(lines) else '').split()
if not parts: continue
cmd=parts[0]
if cmd=='SWAP':
a,b=b,a
else:
x=int(parts[1])
if cmd=='SETA': a=x
elif cmd=='SETB': b=x
elif cmd=='ADDA': a+=x
else: b+=x
sys.stdout.write(f"{a} {b}")
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!