Recursion
Christophe Delord
christophe.delord at free.fr
Fri Jul 19 10:56:14 EDT 2002
On 19 Jul 2002 05:08:58 -0700
karaatanasov at hotmail.com (Kiril Karaatanasov) wrote:
> 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.
>
> Smart but as anything French WRONG !!!!
> try calling
>
> bits(0)
Ok, but the problem was in the specification. That means that the "bug" is a requested feature.
So anything french is not allways wrong ;-)
What about this one (with your patch of course):
bits = lambda x,f=lambda y,g:(y>1 and g(y>>1,g) or "")+"01"[y&1]:f(x,f)
BTW, I know it's not good either because not very readable...
>
> 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]
--
(o_ Christophe Delord _o)
//\ http://christophe.delord.free.fr/ /\\
V_/_ mailto:christophe.delord at free.fr _\_V
More information about the Python-list
mailing list