John Hunter wrote:
> Hey, I did it in 0.526 sec (do I have to count coding time?)
Yes, you have to count coding time :-)
> def is_divisible(x,i):
> v = x/float(i)
> return v==int(v)
You can do that much more cleanly with x % i == 0
% is the 'mod' operator, if you want both the division and the modulus,
use div()
> def digits(n):
> n = str(n)
> digits = []
> for char in n:
> digits.append(int(char))
> return digits
That can also be done much more simply, using list comps -
[int(c) for c in str(n)]
> i = 0
> done = 0
> divisor = 17
> while (not done):
> if is_divisible(sum(digits(i)),divisor) and
>is_divisible(sum(digits(i+1)),divisor):
> print (i,i+1)
> done = 1
> i = i+1
You should really either make this a function, or hard-code the values,
not be halfway between by not hardcoding the values but still not making
it be a function. My preference is always to get it working first and
generalize later.
Also, you're displaying the common bias against advanced flow
control. Everyone thinks that just because goto is a bad idea all forms of
flow control are a bad idea, which is completely incorrect. There is
nothing wrong with break, continue, raise, and returning from the middle
of a function.
As it happens, this bit of code can be cleaned up to where the condition
is put properly in the while clause, but if that couldn't be done, I'd
recommend using a while true and a break rather than setting a flag - the
latter is simply more complicated, with no practical benefit.
Here's the code, a bit updated -
def seventeenish(n):
t = 0
for c in str(n):
t += int(c)
return t % 17 == 0
i = 1
while not seventeenish(i) or not seventeenish(i+1):
i += 1
print (i, i+1)
Thus ends Bram's pedantic coding lesson for today :-)
-Bram
_______________________________________________
Bits mailing list
[EMAIL PROTECTED]
http://www.sugoi.org/mailman/listinfo/bits