Or - a one-liner device to convert a decimal number to a string, using any base, which is lower than 10. If you want to use bases which are above 10, you will have to construct a table somehow that goes from ’0′ to ’9′ and then continues from ‘a’ to the required base, (or you can use a static table). So suppose we are dealing with a base <= 10, we only need to convert it to ascii, so it’s pretty simple.
If you didn’t figure it out until now (and how could you?) I’m talking about Python here. There is this int() function (actually it’s a class type to be more accurate, its constructor), which converts any string to a decimal number. Say, int(’101′, 2) will result in 5. But the opposite operation is no where to be seen.
The straight forward way is easy:
while(n > 0):
n /= BASE
Though, it’s an ugly way, just to show the principle. We can do it with recursion, and then we don’t need to reverse the result, by a side effect of recursion.
When I decided to write the conversion function just for the fun of it, I wanted it to not use recursion…because with recursion it’s really easy. So why to make our life simple when we do things for learning and sport? Besides, for some people recursion is less intuitive, althought we might argue abou it.
So here’s my first version:
“”.join([str((n/i)%b) for i in [b**j for j in xrange(31, -1, -1)]]).lstrip(’0′)
At the beginning I use chr((n/i)%b + 0×30), because I’m used to deal with char arrays and thinking old school C code. So Kasperle came up with the str thingy, which is much better for code readability.
Anyway, I really got pissed with the idea that I have to drop all leading zero, otherwise for n=5, I will get an input of ’00000000000000000000000000001110′, which is quite cumbersome.
One drawback is the size of integer we want to convert, as you probably guessed, this code supports 32 bit numbers, it might support any number in a jiffy… But then you will probably have to strip more zeros most of the times. ;( Enough fooling around.
What I’m really trying to achieve is to use the code to convert any sized number, without the need of any constant magic value in my one-liner.
So trying to come up with the accurate number of digits to convert in the first place is the really the bugging trick. What we really need is something like math.log. Using the log we can know the number of digits at once. But then we need to import math. Do we count ‘import’s when we say one-liner or not? Well, I will take it as No. Hardening my life without math.
“”.join([str((n/i)%b) for i in [b**j for j in xrange(math.log(n, b), -1, -1)]])
I could have used the input number for the xrange, but it won’t return ’0′ for an input zero number. And even so, it’s kidna cheating and lame.
Technically, the solution is to generate a list with [1, 10, 100, 1000, ....]. The condition to stop is when n/entry == 0. The problem to make this list is how to generate it on the fly? or how to stop generating it.
Well, AFAIK in Python it’s not possible. So I’m trying to simulate log. Imri just suggested to use a rate number for a log approximation which will be base dependent. But I didn’t like that idea – magic numbers, remember? And maybe even losing precision.
By now, Kasperle, who was the recursion guy, lost his patience with my stupid challenge. Imri is trying to calculate crazy numbers for log approximations, which I stopped following long ago.
FYI: Kasperle’s code, which is pretty cool, goes like this:
foo = lambda n,base: (n or “”) and (str(foo( n / base, base)) + str( n % base))
Notice the way the recursion stops…However, in one-liner code, I prefer assigning the result to a value, rather than assign the lambda and call it. But it’s also possible to do, for instance: x = (lambda y: y+1)(0). But if you ask me, I don’t really like this notation.
Then Imri suggested another idea using sqrt, but I objected since we need math. The truth is that you can do x**0.5 in Python. But eventually his solution wasn’t good enough.
ARRRG, As for now I am giving up . If you have another idea, let me know.