[Tutor] n.isalnum() is failing

Alan Gauld alan.gauld at btinternet.com
Thu Jul 5 11:17:16 CEST 2007


"János Juhász" <janos.juhasz at VELUX.com> 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. 




More information about the Tutor mailing list