Created
October 24, 2017 19:42
-
-
Save hoanbka/9ffa7cbc204fdac29f13fedecf1a102c to your computer and use it in GitHub Desktop.
sum of digits in a string
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
def sumOfDigits(str): | |
temp = '' | |
sum = 0 | |
for i in range(len(str)): | |
if str[i].isdigit(): | |
temp += str[i] | |
else: | |
if temp.isdigit(): | |
sum += int(temp) | |
temp = '' | |
if temp.isdigit(): | |
sum += int(temp) | |
return sum | |
#TEST | |
str = '123adgg2sds3' | |
print(sumOfDigits(str)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment