Count Valid Integers
Python
Medium
4 views
Problem Description
Read n tokens (may contain junk). Count how many tokens are valid integers and output the count.
Input Format
First line n. Second line has n tokens.
Output Format
One integer count.
Sample Test Case
Input:
6
10 x -3 4.5 0 +7
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if len(lines)<2:
sys.stdout.write('0')
else:
try:
n=int(lines[0].strip())
except Exception:
sys.stdout.write('0')
raise SystemExit
toks=lines[1].split()[:n]
c=0
for t in toks:
try:
int(t)
c+=1
except Exception:
pass
sys.stdout.write(str(c))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!