Sum With Bad Tokens
Python
Medium
3 views
Problem Description
Read n tokens. If token is a valid signed integer, add it to sum. Otherwise ignore it. Output the sum.
Input Format
First line n. Second line n tokens.
Output Format
One integer sum.
Sample Test Case
Input:
6
10 x -3 4.5 7 +2
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]
sm=0
for s in arr:
if re.fullmatch(r'[+-]?\\d+',s):
sm+=int(s)
sys.stdout.write(str(sm))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!