Count Token Types
Python
Medium
4 views
Problem Description
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 n. Second line n tokens.
Output Format
One line: intCount floatCount boolCount noneCount wordCount.
Sample Test Case
Input:
8
10 2.5 True hello NONE -7 3.0 False
Official Solution
import sys,re
p=sys.stdin.read().strip().split()
if not p: sys.exit(0)
n=int(p[0])
arr=p[1:1+n]
ci=cf=cb=cn=cw=0
for s in arr:
if s=='True' or s=='False':
cb+=1
elif s=='NONE':
cn+=1
elif re.fullmatch(r'[+-]?\\d+',s):
ci+=1
elif re.fullmatch(r'[+-]?(?:\\d+\\.\\d*|\\d*\\.\\d+)',s):
cf+=1
else:
cw+=1
sys.stdout.write(f'{ci} {cf} {cb} {cn} {cw}')
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!