Live data from Hacker News

Nastiest Python list comprehension ever

blog.garlicsim.org

11–20 of 55 posts

Re: Nastiest Python list comprehension ever

#12
post #10
post #5

> 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)

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.

This 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-2to...

Re: Nastiest Python list comprehension ever

#13
post #10
post #5

> 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)

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.

The `list(range(n))` is for Python 3 compatibility :)

The [:] you mentioned is indeed redundant, I removed it. Thanks.

Re: Nastiest Python list comprehension ever

#14
post #10
post #5

> 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)

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.

list(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?

Re: Nastiest Python list comprehension ever

#15
post #10
post #5

> 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)

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.

list(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.

Re: Nastiest Python list comprehension ever

#17
post #5

> 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)

     (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.

Re: Nastiest Python list comprehension ever

#18

Can somebody from the Perl community please but this guy in the right place by supplying a one-liner Perl golf quadruple map call?

Good point. "Least readable Python code" is a distinction similar to "smallest market cap on the Fortune 50" or "fattest Olympic decathlete."

Re: Nastiest Python list comprehension ever

#19

Can somebody from the Perl community please but this guy in the right place by supplying a one-liner Perl golf quadruple map call?

You mean like this implementation of the Sieve of Eratosthenes?

  sub sieve3 {
      grep{@_[map$a*$_,$_..@_/($a=$_)]=0if$_[$_]>1}@_=0..pop
  }
I shamelessly stole that from http://www.perlmonks.org/index.pl/?node_id=81769. If you just want a short solution, but don't care about the algorithm, then the following works:

  sub sieve {
      sub p{$_[0],@_>1?p(grep$_%$_[0],@_):1}p 2..pop
  }
See http://www.perlmonks.org/index.pl/?node_id=81771 for the original.

Re: Nastiest Python list comprehension ever

#20
post #19

Can somebody from the Perl community please but this guy in the right place by supplying a one-liner Perl golf quadruple map call?

You mean like this implementation of the Sieve of Eratosthenes? sub sieve3 { grep{@_[map$a*$_,$_..@_/($a=$_)]=0if$_[$_]>1}@_=0..pop } I shamelessly stole that from http://www.perlmonks.org/index.pl/?node_id=81769 . If you just want a short solution, but don't care about the algorithm, then the following works: sub sieve { sub p{$_[0],@_>1?p(grep$_%$_[0],@_):1}p 2..pop } See http://www.perlmonks.org/index.pl/?node_id=…

Wow... and I was happy with:

    sub mystery{for($t=3;$t*$t
(Based on http://www.c2.com/cgi/wiki?SieveOfEratosthenesInManyProgramm... but with a fun push alternative and in function form)
Post reply on HN