John Machin <[EMAIL PROTECTED]> wrote:

> Here's a sketch; I'll leave you to fill in the details -- you may wish
> to guard against interesting input like b < 2.
> 
>>>> def anybase(n, b, digits='0123456789abcdef'):
> ...    tmp = []
> ...    while n:
> ...       n, d = divmod(n, b)
> ...       tmp.append(digits[d])
> ...    return ''.join(reversed(tmp))
> ...

Nice. Here's a one-line version based on your code (with added check for 
negative n):

def anybase(n, b, digits='0123456789abcdef'):
        return ('-'+anybase(-n,b) if n < 0
                else '0' if n==0
                else ''.join([digits[d] for (n,d) in iter(lambda:divmod(n,b),
(0,0))])[::-1])

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to