Recursion
Kiril Karaatanasov
karaatanasov at hotmail.com
Fri Jul 19 08:08:58 EDT 2002
Christophe Delord <christophe.delord at free.fr> wrote in message news:<20020719083105.5c01b6a4.christophe.delord at free.fr>...
> Try this:
>
> def bits(x):
> return (x!=1 and bits(x>>1) or "") + "01"[x&1]
>
> The idea is to return the string instead of printing it.
>
> or this if you prefer:
>
> def bits(x):
> if x != 1:
> s = bits(x>>1)
> if x & 1:
> s = s+"1"
> else
> s = s+"0"
> return s
Smart but as anything French WRONG !!!!
try calling
bits(0)
Now may be we want to write it the good way vs. the "smart" way :o)
def bits(x):
return (x>1 and bits(x>>1) or "") + "01"[x&1]
More information about the Python-list
mailing list