Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
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.
# Comprehension:
# return sum(ch.isdigit() for ch in text)
# Map:
return sum(map(str.isdigit, text))
July 26, 2021
Comments: