4 ms·
Yes, 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
by srean 16y ago
Yes, 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.