Created
July 7, 2020 15:48
-
-
Save carlosble/078a3e8c2691e3da495bb6d94f0d2334 to your computer and use it in GitHub Desktop.
String calculator Kata 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def sum_numbers_in(expression: str) -> int: | |
if expression is None or expression == "": | |
return 0 | |
if "," in expression: | |
tokens = expression.split(',') | |
total = 0 | |
for token in tokens: | |
total = total + parse_int(token) | |
return total | |
return parse_int(expression) | |
def parse_int(token): | |
if re.match("^[0-9]+$", token): | |
return int(token) | |
return 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment