How to exclude characters when counting digits - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: How to exclude characters when counting digits (/thread-11289.html) Pages:
1
2
|
RE: How to exclude characters when counting digits - buran - Jul-03-2018 (Jul-03-2018, 11:27 AM)gruntfutuk Wrote: I didn't run time it. import timeit import string my_string = 'DS80A98SD09a8SD098ASD098A09sd8c0a9SCNAfsdg8fbvdfad098098f00A'*1000 def count_func(s): return sum(c.isdigit() for c in s) def smart_count_digits(my_string): orig_length = len(my_string) for digit in string.digits: my_string = my_string.replace(digit, '') return orig_length - len(my_string) elapsed = timeit.timeit('count_func(my_string)', number=1000, setup='from __main__ import count_func, my_string') print( 'count_func took {:.3f} sec'.format(elapsed)) elapsed = timeit.timeit('smart_count_digits(my_string)', number=1000, setup='from __main__ import smart_count_digits, my_string') print('smart_count_digits took {:.3f} sec'.format(elapsed))
RE: How to exclude characters when counting digits - gruntfutuk - Jul-03-2018 (Jul-03-2018, 11:59 AM)buran Wrote: Brilliant. Thanks for the timings. I expect my original was probably also faster. I'm going to have to start timing things more often. RE: How to exclude characters when counting digits - ichabod801 - Jul-03-2018 def ick_count(text): return len(text) - sum([text.count(digit) for digit in string.digits])
RE: How to exclude characters when counting digits - DeaD_EyE - Jul-03-2018 (Jul-03-2018, 11:27 AM)gruntfutuk Wrote:def count_func(s): return sum(c.isdigit() for c in s)would be pretty efficient, but I didn't run time it. I like this solution also, but I can't test it. My CPU Speed is not fixed, so I get not usable results. I guess the methodcall isdigit() takes bit time.Maybe this can be faster: import string def count_func(s): return sum(1 for c in s if c in set(string.digits))But again, I can't benchmark it :-/ RE: How to exclude characters when counting digits - ichabod801 - Jul-03-2018 digit_re = re.compile('\d+') def re_count(text): return len(text) - len(digit_re.sub(text, '')) Edit: Function gave wrong answer, corrected function and reran times.
RE: How to exclude characters when counting digits - ichabod801 - Jul-03-2018 On further examination, both my functions are giving the wrong answer, so they shouldn't be considered. RE: How to exclude characters when counting digits - buran - Jul-03-2018 (Jul-03-2018, 01:29 PM)ichabod801 Wrote: On further examination, both my functions are giving the wrong answer, so they shouldn't be considered. I'm on comute, so would you timeit this one def ick_count(text): return sum(text.count(digit) for digit in string.digits) RE: How to exclude characters when counting digits - gruntfutuk - Jul-03-2018 On my iPad: print('smart:',timeit.timeit("smart_count_digits(my_string)", setup = "from __main__ import smart_count_digits, my_string", number = 1000)) print('count:',timeit.timeit("count_digits(my_string)", setup = "from __main__ import count_digits, my_string", number = 1000)) print('ick:',timeit.timeit("ick_count(my_string)", setup = "from __main__ import ick_count, my_string", number = 1000)) ick_count is the winner.
RE: How to exclude characters when counting digits - volcano63 - Jul-03-2018 After a lecture recently delivered in another forum about the evilness of the non-functional ways , I started playing around with 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
RE: How to exclude characters when counting digits - volcano63 - Jul-03-2018 (Jul-03-2018, 01:13 PM)DeaD_EyE Wrote:import string def count_func(s): return sum(1 for c in s if c in set(string.digits)) It may be re-written as def count_func(s): return sum(c in set(string.digits) for c in s)You can add booleans as integers. Still, major loser to @ichabod801 With a little tweak - making set of digits "static"
|