JSON Type Counter
Programming Interview
Hard
6 views
Problem Description
A JSON array is provided in one line, like [1,2,true,null]. Count how many numbers, strings, booleans, and null values. Output counts in order: num str bool null.
Input Format
One line JSON array.
Output Format
One line: num str bool null.
Sample Test Case
Input:
[1,2,true,null,false,3]
Official Solution
import sys,json
s=sys.stdin.read().strip()
if not s: sys.exit(0)
a=json.loads(s)
cn=cs=cb=cz=0
for v in a:
if v is None:
cz+=1
elif isinstance(v,bool):
cb+=1
elif isinstance(v,(int,float)):
cn+=1
elif isinstance(v,str):
cs+=1
sys.stdout.write(f'{cn} {cs} {cb} {cz}')
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!