Solve a Debate

Steve Holden steve at holdenweb.com
Sat Feb 16 00:50:27 EST 2008


Dan Bishop wrote:
> On Feb 15, 10:24 am, nexes <nexes... at gmail.com> wrote:
>> Alright so me and my friend are having argument.
>>
>> Ok the problem we had been asked a while back, to do a programming
>> exercise (in college)
>> That would tell you how many days there are in a month given a
>> specific month.
>>
>> Ok I did my like this (just pseudo):
>>
>> If month = 1 or 3 or etc ....
>>         noDays = 31
>> Elseif month = 4 or 6 etc ....
>>         noDays = 30
>> Else
>>         noDays = 29
>> (we didn't have to take into account a leap year)
>>
>> He declared an array and assigned the number of days in a month to its
>> own element in an array. Now
>> I realise that in this example it would not make a difference in terms
>> of efficiency, but it is my belief that if
>> there is more data that needed to be assigned(i.e. a couple megs of
>> data) it would be simpler (and more efficient) to
>> do a compare rather then assigning all that data to an array, since
>> you are only going to be using 1 value and the rest
>> of the data in the array is useless.
>>
>> What are everyone else's thoughts on this?
> 
> days_in_month = lambda m: m - 2 and 30 + bool(1 << m & 5546) or 28

Elegant, but the 5546 is way too magical for readability.

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list