Parallel Assignment Simulator
Python
Hard
2 views
Problem Description
You have two variables a and b. For each step you get newA and newB expressions in terms of old a and b: 'A' means old a, 'B' means old b, or an integer. Apply parallel assignment each step (both update together). Output final a b.
Input Format
Line1: a b. Line2: m. Next m lines: exprA exprB.
Output Format
One line: a b.
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if not lines or not lines[0].strip():
sys.exit(0)
a0,b0=map(int,lines[0].split())
m=int(lines[1].strip())
a=a0; b=b0
for i in range(m):
e=(lines[2+i] if 2+i<len(lines) else '').split()
if len(e)<2: continue
def evalExpr(tok,oa,ob):
if tok=='A':
return oa
if tok=='B':
return ob
return int(tok)
oa,ob=a,b
na=evalExpr(e[0],oa,ob)
nb=evalExpr(e[1],oa,ob)
a,b=na,nb
sys.stdout.write(f"{a} {b}")
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!