After a lecture recently delivered in another forum about the evilness of the non-functional ways , I started playing around with
So, adding map to @ichabod801's solution should have brought salvation ? Apparently not
map
, filter
and reduce
(I was using them occasionally before, but I decided to raise my level of involvement)So, adding map to @ichabod801's solution should have brought salvation ? Apparently not
Output:In [4]: def ick_count(text):
...: return sum(text.count(digit) for digit in string.digits)
...:
...: def ick_map_count(text):
...: return sum(map(text.count, string.digits))
...:
...:
In [5]: %timeit ick_count(my_string)
423 µs ± 6.69 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [6]: %timeit ick_map_count(my_string)
423 µs ± 10.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
Test everything in a Python shell (iPython, Azure Notebook, etc.)
- Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.