-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sum Numbers.py
29 lines (23 loc) · 1010 Bytes
/
Sum Numbers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""
https://py.checkio.org/ru/mission/sum-numbers/
In a given text you need to sum the numbers.
Only separated numbers should be counted. If a number is part of a word it shouldn't be counted.
The text consists from numbers, spaces and english letters
Input: A string.
Output: An int.
"""
def sum_numbers(text: str) -> int:
return sum(int(elem) for elem in text.split() if elem.isdigit())
if __name__ == '__main__':
print("Example:")
print(sum_numbers('hi'))
# These "asserts" are used for self-checking and not for an auto-testing
assert sum_numbers('hi') == 0
assert sum_numbers('who is 1st here') == 0
assert sum_numbers('my numbers is 2') == 2
assert sum_numbers('This picture is an oil on canvas '
'painting by Danish artist Anna '
'Petersen between 1845 and 1910 year') == 3755
assert sum_numbers('5 plus 6 is') == 11
assert sum_numbers('') == 0
print("Coding complete? Click 'Check' to earn cool rewards!")