Count Digits In Each Line
Python
Easy
4 views
Problem Description
Read t lines. For each line, count how many characters are digits (0-9) and output the count.
Input Format
First line t. Next t lines are strings.
Output Format
t lines counts.
Sample Test Case
Input:
3
ab12
9x9x
no digits
Constraints
Total characters over all lines
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if not lines:
sys.exit(0)
try:
t=int(lines[0].strip())
except Exception:
t=0
out=[]
idx=1
for _ in range(t):
s=lines[idx] if idx<len(lines) else ''
idx+=1
c=0
for ch in s:
o=ord(ch)
if 48<=o<=57:
c+=1
out.append(str(c))
sys.stdout.write('\
'.join(out))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!