Key Value Store (Ignore Bad Lines)
Python
Medium
3 views
Problem Description
Read n lines with format key=value. Some lines may not have '=' or may be empty, ignore them. Then q queries keys. For each query output value or NOT FOUND.
Input Format
First line n. Next n lines. Next line q. Next q lines keys.
Output Format
q lines values.
Sample Test Case
Input:
4
a=10
badline
b=hello
=
3
a
b
c
Output:
10
hello
NOT FOUND
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if not lines: sys.exit(0)
idx=0
n=int(lines[idx].strip()); idx+=1
mp={}
for _ in range(n):
if idx>=len(lines):
break
line=lines[idx]; idx+=1
if '=' not in line:
continue
k,v=line.split('=',1)
if not k:
continue
mp[k]=v
if idx>=len(lines):
sys.stdout.write('')
raise SystemExit
q=int(lines[idx].strip()); idx+=1
out=[]
for _ in range(q):
if idx>=len(lines):
break
k=lines[idx].strip(); idx+=1
out.append(mp.get(k,'NOT FOUND'))
sys.stdout.write('\
'.join(out))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!