6 ms·
> Does anyone have a suggestion on how to do it? Yes, provide more details so I don't have to spend twenty minutes figuring out what it does. (Going by my thr
by StavrosK 16y ago
> Does anyone have a suggestion on how to do it?
Yes, provide more details so I don't have to spend twenty minutes figuring out what it does.
(Going by my three-second-look gut feeling: It's the Sieve of Eratosthenes)
- sibsibsib 16y agoit generates a list of prime numbers up to 'n', including some false positives. The author wasn't kidding about it being nasty to read...
- StavrosK 16y agoLooking at it a bit more, it's sort-of-obvious that that's what it is, if you've seen the Sieve before. However: > i for a[::i] in <whatever> Mind blown. How does assigning to an array even work? I'm guessing this is way-undefined-speak for setting elements, and I am simultaneously dazzled by its brilliance and horrified by its stupidity.
- cool-RR 16y agoYes, that was the desired effect :)
- zem 16y agoyeah, i figured out what it did by playing in the repl, but i still can't find it documented anywhere.
- StavrosK 16y agoI tried a bit in the REPL and couldn't replicate it completely, but I didn't try hard. It seems to only work because of the CPython internals, so it seems to be implementation-specific and undocumented.
- cool-RR 16y agoNot true, it's part of Python's syntax. Here it is in Pypy: Python 2.5.2 (79560, Nov 26 2010, 14:57:55) [PyPy 1.4.0] on win32 >>> a = [1, 2 ,3] >>> for a[:] in [[6, 7, 8]]: ... pass >>> print(a) [6, 7, 8]
- StavrosK 16y agoSo what does it do? Run the expression first and assign to whatever's there? It seems that it would only work to assign to a slice, as anything else would be overwritten, but why?
- srean 16y agoYes, but I could not understand why one needs list(range(n)) as opposed to range(n) and why the double indexing of a in i in a[:][2:] if a[i] == i]. Am I missing something ? I think it would work without those. Edit: Yo La Tengo. Deviousness aside the code is quite nice. It would be fun to optimize it but without breaking its spirit.
- cookiecaper 16y agoThis is probably a Python 3 program. The list() wrapped around range() makes a list out of an iterator -- range() now returns an iterator in Py 3. See http://diveintopython3.org/porting-code-to-python-3-with-2to3.html#xrange http://diveintopython3.org/porting-code-to-python-3-with-2to...
- cool-RR 16y agoThe `list(range(n))` is for Python 3 compatibility :) The [:] you mentioned is indeed redundant, I removed it. Thanks.
- sophacles 16y agolist(range(n)) -- I sometimes do this because I never can remember if they have finally gotten around to making range a generator yet, and better safe than sorry. a[:][2:] -- maybe the author doesn't know that any slice returns a new list? Maybe he just wants to be even more mysterious?
- StavrosK 16y agolist(range(n)) is probably because the author was using Python 3, in which range() returns an iterator (think xrange in 2.x). The double indexing is probably because a[:] returns a copy of a and the author doesn't want to iterate over the modified array, since he modifies it down the line (although the [2:] would copy it too, so no idea). An alternative to a[:][2:] would just be range(2, n), but the author clearly doesn't want it to be readable.
- kingkilr 16y ago(Going by my three-second-look gut feeling: It's the Sieve of Eratosthenes) A broken one, this seems like advertising that you hole in one'd a sand trap.