Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Easy to read solution in Clear category for Count Digits by BootzenKatzen
# I can see that their solution is shorter... but I think mine's easier to understand.
# I know they're trying to get us to use new functions like map() but... do I need to?
def count_digits(text: str) -> int:
count = 0
for bit in text:
if bit.isdigit():
count = count + 1
return count
print("Example:")
print(count_digits("hi"))
# These "asserts" are used for self-checking
assert count_digits("hi") == 0
assert count_digits("who is 1st here") == 1
assert count_digits("my numbers is 2") == 1
assert (
count_digits(
"This picture is an oil on canvas painting by Danish artist Anna Petersen between 1845 and 1910 year"
)
== 8
)
assert count_digits("5 plus 6 is") == 2
assert count_digits("") == 0
print("The mission is done! Click 'Check Solution' to earn rewards!")
March 30, 2023
Comments: