patternpythonMinor
Get count of digits of a number that divide the number
Viewed 0 times
dividenumberthedigitsgetthatcount
Problem
So, here's a program in python to get the count of digits that divide the number. If you give the program a list like
This is the code:
The above code obviously works. How can I further optimize this, make it short or do it differently?
[24,10245], you get an output of:2
2This is the code:
def count_div_digits(l):
for number in l:
count=0
digits=str(number)
for digit in digits:
if digit=='0':
continue
else:
if number%(int(digit))==0:
count+=1
print countThe above code obviously works. How can I further optimize this, make it short or do it differently?
Solution
Do one thing
Let's start with your top level:
Firstly,
Return, don't
Right now, your
And this way, you can store the results somewhere else for further analysis later.
Avoid
The less weird hops your loops make, the easier it is to follow along the logic. Right now you have:
But it'd be clearer to just do:
Proposed Solution
Generator Expressions
You could also write the above with the help of a generator expression by factoring out a length:
Whether you consider that an improvement or not is a matter of opinion.
Let's start with your top level:
def count_div_digits(l):
for number in l:Firstly,
l is a terrible variable name. It looks like 1. Avoid it at all costs. Secondly, this function is less useful than it could be simply because it requires a list of numbers. It'd be better if the function took a single number, then you could call it on a list separately if you want:def count_div_digits(number):
...
for number in lst:
count_div_digits(number)Return, don't
printRight now, your
count_div_digits simply prints its result. That's all well and good, but it'd be better if you just returned the number. If you want to print it, you can always just do:print count_div_digits(number)And this way, you can store the results somewhere else for further analysis later.
Avoid
continue:The less weird hops your loops make, the easier it is to follow along the logic. Right now you have:
if digit=='0':
continue
else:
# stuffBut it'd be clearer to just do:
if digit != '0':
# same stuffProposed Solution
def count_div_digits(number):
count = 0
for digit in str(number):
if digit != '0' and number % int(digit) == 0:
count += 1
return countGenerator Expressions
You could also write the above with the help of a generator expression by factoring out a length:
def sizeof(iterable):
return sum(1 for _ in iterable)
def count_div_digits(number):
return sizeof(digit for digit in str(number)
if digit != '0' and number % int(digit) == 0)Whether you consider that an improvement or not is a matter of opinion.
Code Snippets
def count_div_digits(l):
for number in l:def count_div_digits(number):
...
for number in lst:
count_div_digits(number)print count_div_digits(number)if digit=='0':
continue
else:
# stuffif digit != '0':
# same stuffContext
StackExchange Code Review Q#105274, answer score: 7
Revisions (0)
No revisions yet.