JSON Type Counter

JSON Type Counter

Hard Python Data Types 14 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

• First line: A JSON array in one line
• Example: [1,2,true,null]

Output Format

• Print four integers separated by space:

• Count of numbers

• Count of strings

• Count of booleans

• Count of null values

Order:
num str bool null

Example

[1,"hello",true,null,5,false,"abc"]
2 2 2 1

Constraints

• Input is a valid JSON array

• Length ≤ 10^5

Concept Explanation

Array elements:

1 → number
"hello" → string
true → boolean
null → null
5 → number
false → boolean
"abc" → string

Counts:
Numbers = 2
Strings = 2
Booleans = 2
Null = 1

Step-by-Step Explanation

1.Read the input line as string.

2.Use json.loads() to parse it into a Python list.

3.Initialize counters:

• num = 0

• str_c = 0

• bool_c = 0

• null_c = 0

4.Loop through each element in the list:

• If type is int or float → increase num.

• If type is str → increase str_c.

• If type is bool → increase bool_c.

• If value is None → increase null_c.

5.Print counts in order:
num str_c bool_c null_c.

Concept Explanation

Array elements:

1 → number
"hello" → string
true → boolean
null → null
5 → number
false → boolean
"abc" → string

Counts:
Numbers = 2
Strings = 2
Booleans = 2
Null = 1

Step-by-Step Explanation

1.Read the input line as string.

2.Use json.loads() to parse it into a Python list.

3.Initialize counters:

• num = 0

• str_c = 0

• bool_c = 0

• null_c = 0

4.Loop through each element in the list:

• If type is int or float → increase num.

• If type is str → increase str_c.

• If type is bool → increase bool_c.

• If value is None → increase null_c.

5.Print counts in order:
num str_c bool_c null_c.

Input / Output Format

Input Format
• First line: A JSON array in one line
• Example: [1,2,true,null]
Output Format
• Print four integers separated by space:

• Count of numbers

• Count of strings

• Count of booleans

• Count of null values

Order:
num str bool null
Constraints
• Input is a valid JSON array

• Length ≤ 10^5

Examples

Input:
[1,"hello",true,null,5,false,"abc"]
Output:
2 2 2 1

Example Solution (Public)

Python
import sys,jsonns=sys.stdin.read().strip()nif not s: sys.exit(0)na=json.loads(s)ncn=cs=cb=cz=0nfor v in a:n  if v is None:n    cz+=1n  elif isinstance(v,bool):n    cb+=1n  elif isinstance(v,(int,float)):n    cn+=1n  elif isinstance(v,str):n    cs+=1nsys.stdout.write(f'{cn} {cs} {cb} {cz}')

Official Solution Code

import sys,jsonns=sys.stdin.read().strip()nif not s: sys.exit(0)na=json.loads(s)ncn=cs=cb=cz=0nfor v in a:n  if v is None:n    cz+=1n  elif isinstance(v,bool):n    cb+=1n  elif isinstance(v,(int,float)):n    cn+=1n  elif isinstance(v,str):n    cs+=1nsys.stdout.write(f'{cn} {cs} {cb} {cz}')
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.