Find First Unset Variable
Python
Hard
2 views
Problem Description
You will receive q queries like GET name or SET name value. Maintain variables in a dict. For each GET, output value if present else UNSET.
Input Format
First line q. Next q lines: commands.
Output Format
Outputs for each GET.
Sample Test Case
Input:
6
GET a
SET a 10
GET a
SET b -2
GET b
GET c
Output:
UNSET
10
-2
UNSET
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if not lines or not lines[0].strip():
sys.exit(0)
q=int(lines[0].strip())
vars={}
out=[]
for i in range(1,1+q):
parts=(lines[i] if i<len(lines) else '').split()
if not parts: continue
if parts[0]=='SET':
vars[parts[1]]=parts[2]
else:
name=parts[1]
out.append(vars.get(name,'UNSET'))
sys.stdout.write('\
'.join(out))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!