Fun with fancy slicing
Michael Hudson
mwh at python.net
Tue Sep 30 10:38:23 EDT 2003
Dave Benjamin <ramen at lackingtalent.com> writes:
> Anyone have any other interesting applications of the extended slice syntax?
You can use it in a sieve prime finder (ooh, thread crossing :-):
/>> def sieve(p):
|.. candidates = range(p)
|.. for i in candidates[2:]:
|.. if not candidates[i]: continue
|.. n = len(candidates[2*i::i])
|.. candidates[2*i::i] = [0]*n
|.. return filter(None, candidates[2:])
\__
->> sieve(50)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
but it's not especially efficient (in speed or memory). Quite cute,
though.
Cheers,
mwh
--
A typical luser can perform truly superhuman feats of strength &
dexterity to avoid reading documentation. -- Lionel, asr
More information about the Python-list
mailing list