Nastiest Python list comprehension ever
11–20 of 55 posts
Re: Nastiest Python list comprehension ever
#12> 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.
Re: Nastiest Python list comprehension ever
#13> 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 [:] you mentioned is indeed redundant, I removed it. Thanks.
Re: Nastiest Python list comprehension ever
#14> 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.
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> 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.
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
#16Bad BDFL, bad!
Re: Nastiest Python list comprehension ever
#17> 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
#18Can somebody from the Perl community please but this guy in the right place by supplying a one-liner Perl golf quadruple map call?
Re: Nastiest Python list comprehension ever
#19Can somebody from the Perl community please but this guy in the right place by supplying a one-liner Perl golf quadruple map call?
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
#20Can 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=…
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)