JSON Type Counter

JSON Type Counter

Hard Programming Interview Data Structures 22 views
Explanation Complexity

Problem Statement

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.

Example

[1,2,true,null,false,3]
3 0 2 1

Constraints

JSON length

Input / Output Format

Input Format
One line JSON array.
Output Format
One line: num str bool null.
Constraints
JSON length

Examples

Input:
[1,2,true,null,false,3]
Output:
3 0 2 1

Example Solution (Public)

Programming Interview
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}')

Official Solution Code

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}')
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.