Variable Store (SET/ADD/MUL)
Python
Medium
2 views
Problem Description
You will receive q commands on variable x: SET v, ADD v, MUL v. Start x=0. After all commands output x.
Input Format
First line q. Next q lines: command value.
Output Format
One integer x.
Sample Test Case
Input:
4
SET 2
ADD 3
MUL 10
ADD -5
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())
x=0
for i in range(q):
parts=(lines[i+1] if i+1<len(lines) else '').split()
if not parts: continue
cmd=parts[0]
v=int(parts[1])
if cmd=='SET':
x=v
elif cmd=='ADD':
x+=v
else:
x*=v
sys.stdout.write(str(x))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!