[Tutor] Re: How to convert an integer into a binary?
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>=b:
> > l.insert(1,0)
> > l,l=divmod(l,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...
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... :-)