[Tutor] Re: How to convert an integer into a binary?
alan.gauld@bt.com
alan.gauld@bt.com
Wed, 8 Aug 2001 17:48:41 +0100
> On Wed, Aug 08, 2001 at 08:43:20AM -0500, Christopher Smith wrote:
> > what I came up with:
> >
> > def d2b(numb,b=2):
> > '''Returns a list containing the base b representation of
> > the decimal number, big end first.
> > '''
> > l=[numb]
> > while l[0]>=b:
> > l.insert(1,0)
> > l[0],l[1]=divmod(l[0],b)
> > return l
Ooh, thats nice! It was sufficiently clever I had to insert
a couple of print statements round the divmod to check how
it worked. Interesting use of insert, I like it...
Alan g
PS In my previous attempt at this I forgot the reverse operation
at the end, sorry, that was careless. But having seen this
solution it becomes irrelevant... :-)