Map solution in Clear category for Count Digits by HeNeArKr
def count_digits(text: str) -> int:
"""Return count of number of digits contained in text."""
# Two important points:
# 1) Class bool is subclass of int
# 2) A str is iterable
# Tried 2 versions; compared their performance with timeit.
# Map version was more than twice as fast, at least under the
# specific test conditions.
# return sum(ch.isdigit() for ch in text)
return sum(map(str.isdigit, text))
July 26, 2021