Parse Key:Value Pairs
Python
Medium
2 views
Problem Description
One line has pairs like key:value separated by spaces. Build dictionary and output how many pairs were parsed and sum of all integer values.
Input Format
One line: pairs.
Output Format
One line: count sum.
Constraints
Total characters
Official Solution
import sys
s=sys.stdin.read().strip()
if not s: sys.exit(0)
mp={}
sm=0
cnt=0
for part in s.split():
if ':' not in part:
continue
k,v=part.split(':',1)
if k=='':
continue
mp[k]=v
cnt+=1
try:
sm+=int(v)
except:
pass
sys.stdout.write(f'{cnt} {sm}')
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!