Solve a Debate

castironpi at gmail.com castironpi at gmail.com
Sat Feb 16 20:43:37 CET 2008


On Feb 15, 11:50 pm, Steve Holden <st... at holdenweb.com> wrote:
> 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.

>>> int( '0101010110101'[::-1], 2 )
5546



More information about the Python-list mailing list