Solve a Debate

John Machin sjmachin at lexicon.net
Sat Feb 16 16:43:37 EST 2008


On Feb 16, 3:48 pm, Dan Bishop <danb... at yahoo.com> 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

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)




More information about the Python-list mailing list