Solve a Debate

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Feb 16 19:11:05 EST 2008


En Sat, 16 Feb 2008 19:43:37 -0200, John Machin <sjmachin at lexicon.net>  
escribi�:
> On Feb 16, 3:48 pm, Dan Bishop <danb... at yahoo.com> wrote:
>>
>> days_in_month = lambda m: m - 2 and 30 + bool(1 << m & 5546) or 28
>
> Alternatively:
>
> days_in_month = lambda m: m - 2 and 31 - ((m + 9) % 12 % 5 % 2) or 28
>
> the guts of which is slightly more elegant than the ancient writing
> from which it was derived:
>
> MOD(MOD(MOD(M+9,12),5),2)

I wonder why one would write such arcane expressions [in Python] instead  
of using a small dictionary {1:31, 2:28, 3:31...} which is auto  
documented, obviously correct, several times faster, and provides input  
validation for free (days_in_month(13)?)

-- 
Gabriel Genellina




More information about the Python-list mailing list