base converter
Alex Martelli
aleaxit at yahoo.com
Fri Jun 15 06:37:11 EDT 2001
"Omni Permeable" <om at nikocity.de> wrote in message
news:c7cebb43.0106141326.589577e3 at posting.google.com...
...
> > > def BaseConvert(x, b):
> > > "convert decimal number x to base b"
...
> > def BaseConvert(x, b):
> > import string
> > digits = string.digits + string.uppercase
> > if b>len(digits):
> > raise ValueError, "base %s too large"%b
...
> tracking down bug#2 : b==1 iterates forever! because : divmod (5,1) ==
(5,0)
Good point!
> any more elegant solution than this one :
>
> elif b==1:
> return '0'*x
That wouldn't be correct anyway. 1 is just not an acceptable
base -- neither are 0 and -1; you can't represent whatever
integer Z as a polynomial in base 1, 0, or -1. (I have not
checked how BaseConvert behaves with other <-1 bases, or
non-integer bases, or...:-). Just throw a ValueError with
"base %s incorrect"%b for b<2, I'd say.
Alex
More information about the Python-list
mailing list