Hi Alan, Yes! That is succinct and sweet! I think I like that one the best of all!
Terry -------- Forwarded Message -------- From: Alan Gauld <[EMAIL PROTECTED]> To: tutor@python.org Subject: Re: [Tutor] n.isalnum() is failing Date: Thu, 5 Jul 2007 10:17:16 +0100 "János Juhász" <[EMAIL PROTECTED]> wrote >> def isLeapYear(y): >> if y % 4 == 0: return True > As it always return True, if y%4 == 0, there is problem with the > exceptions My original function had %400 not %4 so it worked. >> if (y % 4 == 0) and not (y %100 == 0): return True >> else: return False > > > I feel that, the cleanest way to translate the definition into > Boolean > logic is to do it backward instead of thinking on the exceptions. > > def leap_year(year): > if year%400 == 0: return True # these are always leap year > if year%100 == 0: return False # the exception handled already > if year%4 == 0: return True # no problem with the exceptions > return False # this it the default But I rather like this one since the combined and/not expression is less clear and more prone to confusion.. And in fact it can be simplified even further: def isLeapYear(y): if y % 400: return True if y % 100: return False return y % 4 == 0 > hungarians name format: family name, christian name > hungarian date format: year/month/day > Your logic is backward, and mine is the forward, isn't it? ;) :-) Alan G. _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor