Count Token Types

Count Token Types

Medium Python Data Types 12 views
Explanation Complexity

Problem Statement

Read n tokens. Each token can be: signed int, float (with dot), True/False, NONE, or a word. Count how many INT, FLOAT, BOOL, NONE, and WORD tokens and output counts in this order.

Input Format

• First line: Integer n

• Second line: n space-separated tokens

Each token can be:

• Signed integer

• Float (contains one dot)

• True or False

• NONE

• A word

Output Format

• Print five integers separated by space:

• Count of INT

• Count of FLOAT

• Count of BOOL

• Count of NONE

• Count of WORD

(in this exact order)

Example

6
10 3.5 True NONE hello -7
2 1 1 1 1

Constraints

• 1 ≤ n ≤ 10^5

• Tokens contain no spaces

Concept Explanation

Tokens:
10 → INT
3.5 → FLOAT
True → BOOL
NONE → NONE
hello → WORD
-7 → INT

Counts:
INT = 2
FLOAT = 1
BOOL = 1
NONE = 1
WORD = 1

Step-by-Step Explanation

1.Read integer n.

2.Read list of n tokens.

3.Initialize counters: int_c, float_c, bool_c, none_c, word_c = 0.

4.For each token:

• If token is "True" or "False" → increase bool_c.

• Else if token is "NONE" → increase none_c.

• Else if token matches signed integer format (optional +/- and digits only) → increase int_c.

• Else if token contains exactly one dot and remaining parts are digits (with optional sign) → increase float_c.

• Otherwise → increase word_c.

5.Print counts in order:
int_c float_c bool_c none_c word_c.

Concept Explanation

Tokens:
10 → INT
3.5 → FLOAT
True → BOOL
NONE → NONE
hello → WORD
-7 → INT

Counts:
INT = 2
FLOAT = 1
BOOL = 1
NONE = 1
WORD = 1

Step-by-Step Explanation

1.Read integer n.

2.Read list of n tokens.

3.Initialize counters: int_c, float_c, bool_c, none_c, word_c = 0.

4.For each token:

• If token is "True" or "False" → increase bool_c.

• Else if token is "NONE" → increase none_c.

• Else if token matches signed integer format (optional +/- and digits only) → increase int_c.

• Else if token contains exactly one dot and remaining parts are digits (with optional sign) → increase float_c.

• Otherwise → increase word_c.

5.Print counts in order:
int_c float_c bool_c none_c word_c.

Input / Output Format

Input Format
• First line: Integer n

• Second line: n space-separated tokens

Each token can be:

• Signed integer

• Float (contains one dot)

• True or False

• NONE

• A word
Output Format
• Print five integers separated by space:

• Count of INT

• Count of FLOAT

• Count of BOOL

• Count of NONE

• Count of WORD

(in this exact order)
Constraints
• 1 ≤ n ≤ 10^5

• Tokens contain no spaces

Examples

Input:
6 10 3.5 True NONE hello -7
Output:
2 1 1 1 1

Example Solution (Public)

Python
import sys,renp=sys.stdin.read().strip().split()nif not p: sys.exit(0)nn=int(p[0])narr=p[1:1+n]nci=cf=cb=cn=cw=0nfor s in arr:n  if s=='True' or s=='False':n    cb+=1n  elif s=='NONE':n    cn+=1n  elif re.fullmatch(r'[+-]?\d+',s):n    ci+=1n  elif re.fullmatch(r'[+-]?(?:\d+\.\d*|\d*\.\d+)',s):n    cf+=1n  else:n    cw+=1nsys.stdout.write(f'{ci} {cf} {cb} {cn} {cw}')

Official Solution Code

import sys,renp=sys.stdin.read().strip().split()nif not p: sys.exit(0)nn=int(p[0])narr=p[1:1+n]nci=cf=cb=cn=cw=0nfor s in arr:n  if s=='True' or s=='False':n    cb+=1n  elif s=='NONE':n    cn+=1n  elif re.fullmatch(r'[+-]?\d+',s):n    ci+=1n  elif re.fullmatch(r'[+-]?(?:\d+\.\d*|\d*\.\d+)',s):n    cf+=1n  else:n    cw+=1nsys.stdout.write(f'{ci} {cf} {cb} {cn} {cw}')
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.