On 04/30/2015 02:55 PM, Cecil Westerhof wrote:
Because I want the code to work with Python 3 also, the code is now:
def lucky_numbers(n):
"""
Lucky numbers from 1 up-to n
http://en.wikipedia.org/wiki/Lucky_number
"""
if n < 3:
return [1]
sieve = list(range(1, n + 1, 2))
sieve_index = 1
while True:
sieve_len = len(sieve)
if (sieve_index + 1) > sieve_len:
break
skip_count = sieve[sieve_index]
if sieve_len < skip_count:
break
del sieve[skip_count - 1 : : skip_count]
sieve_index += 1
return sieve
It looks like the list in:
sieve = list(range(1, n + 1, 2))
does not have much influence in Python 2. So I was thinking of leaving
the code like it is. Or is it better to check and do the list only
with Python 3?
I'd do something like this at top of the module:
try:
range = xrange
except NameError as ex:
pass
then use range as it is defined in Python3.
if that makes you nervous, then define irange = xrange, and if it gets a
NameError exception, irange = range
--
DaveA
--
https://mail.python.org/mailman/listinfo/python-list