13
\$\begingroup\$

Write the shortest function to convert an integer into a numeric representation given a radix between 2 and 62. e.g.:

to_string(351837066319, 31) => "codegolf"
\$\endgroup\$
  • 2
    \$\begingroup\$ From the example I gather that lower case letters come first, i.e. the digits in base 62 are 0-9,a-z,A-Z in that order? \$\endgroup\$ – sepp2k Feb 5 '11 at 0:07
  • \$\begingroup\$ Yes, exactly, that's it. \$\endgroup\$ – Michiel de Mare Feb 5 '11 at 18:20
  • \$\begingroup\$ at 0:00, dang, perfect. \$\endgroup\$ – Zacharý Aug 27 '17 at 21:02

11 Answers 11

4
\$\begingroup\$

Ruby 1.8 - 75 characters, with recursion.

f=proc{|n,b|(n<b ? "":f[n/b,b])+([*'0'..'9']+[*'a'..'z']+[*'A'..'Z'])[n%b]}

Without recursion

f=proc{|n,b|d=[*'0'..'9']+[*'a'..'z']+[*'A'..'Z'];s=d[n%b];s=d[n%b]+s while(n/=b)>0;s}

(both based on Dogbert's 1.9 solution.)

\$\endgroup\$
4
\$\begingroup\$

Python - 86

from string import*
def t(n,r,s=''):
 while n:s=printable[n%r]+s;n/=r
 return s or'0'

Credit due to Hoa Long Tam for the string import trick

\$\endgroup\$
3
\$\begingroup\$

dc - 43 chars

[sb[58-]s_[lb~dZ39*+dB3<_9+rd0<xrP]dsxxk]sf

We can shorten this a little if we assume the stack contains only the two arguments.

[[58-]s_dSb~dZ39*+dB3<_9+SadLbr0<fLaPc]sf

As a standalone program, we only need 37 characters:

?o[58-]s_[O~dZ39*+dB3<_9+rd0<xrP]dsxx

Instead of using [39+]sz9<z, we simply use Z39*+, which will add 39 for a single digit number, and 78 for a double digit number. Instead of 113, we use B3 (AD also works).

\$\endgroup\$
3
\$\begingroup\$

Python, 93 99

from string import *
d=digits+letters
def t(n,b):
 s=''
 while n>0:s=d[n%b]+s;n/=b
 return s or '0'

EDIT: " or '0'" added for empty string case

\$\endgroup\$
  • \$\begingroup\$ Fails when n = 0, returns an empty string when it should return '0'. Anyway +1 for the string trick \$\endgroup\$ – Juan Feb 5 '11 at 3:52
2
\$\begingroup\$

dc, 61 chars

[sr[lr~rd0<x]dsxxk[39+]sa[58-]sb[d9<ad78<b48+anz0<p]dspxIP]sf

Run as:

dc -e'[sr[lr~rd0<x]dsxxk[39+]sa[58-]sb[d9<ad78<b48+anz0<p]dspxIP]sf' -e'351837066319 31 lfx'

or:

dc -f base.dc -e '351837066319 31 lfx'

Explanation: We take the number and base on the stack. sr saves the base in register r. The recursive function [lr~rd0<x]dsxx decomposes a number TOS into its digits in base register r. The first digit is always 0, removed from the stack by k (set precision, which by default is 0 also, so it's equivalent to a nop). Then, the recursive function [48+d57<ad122<banz0<p]dspx outputs each digit in ASCII, with the help of functions a ([39+]sa) and b ([58-]sb). IP outputs a newline. The function is stored in register f, and can be invoked by lfx.

\$\endgroup\$
  • 1
    \$\begingroup\$ If you move 48+ to the end it saves two (57 and 122 both drop a character). Also a minor oversight is that as a function, you can't assume there is nothing else on the stack, but the problem would be removed if you merged the loops (which would also save a few characters). \$\endgroup\$ – Nabb Feb 7 '11 at 22:17
2
\$\begingroup\$

Ruby - 72 70 59 chars

f=->n,b{(n<b ? "":f[n/b,b])+[*?0..?9,*?a..?z,*?A..?Z][n%b]}

Without recursion, 70 chars

f=->n,b{d=*?0..?9,*?a..?z,*?A..?Z;s=d[n%b];s=d[n%b]+s while(n/=b)>0;s}

Test

irb(main):080:0> f[351837066319, 31]
=> "codegolf"
irb(main):081:0> f[0, 31]
=> "0"
\$\endgroup\$
1
\$\begingroup\$

Haskell, 109 characters

m=divMod
d(0,x)b=[f x]
d(r,x)b=f x:d(m r b)b
f=(!!)$['0'..'9']++['a'..'z']++['A'..'Z']
s x b=reverse$d(m x b)b
\$\endgroup\$
1
\$\begingroup\$

Befunge - 53 x 2 = 106 characters

Or 53 + 46 = 99 characters if you're willing to route other parts of your program through the bottom left.

11p01-\>:11g%\11g/:#v_$>:1+!#v_:45+`!#v_:75*`!#v_   v
       ^            <  ^,    $# +"0"  < +"'"   <-":"<

First place the number to be converted on the stack, then the radix and enter this function from the top-left going right. Will output the string for you (since Befunge doesn't support string variables) and leave from the bottom $ going down. Requires the (1,1) cell for radix storage.

E.g. for the example given put 351837066319 into the input and run:

&56*1+    11p01-\>:11g%\11g/:#v_$>:1+!#v_:45+`!#v_:75*`!#v_   v
                 ^            <  ^,    $# +"0"  < +"'"   <-":"<
                                       @
\$\endgroup\$
1
\$\begingroup\$

Golfscript - 32 chars

{base{.9>39*+.74>58*48--}%''+}:f
\$\endgroup\$
1
\$\begingroup\$

Ruby 1.9 - 80 74 68

t=->n,b{d=?0..?9,?a..?z,*?A..?Z;s='';(s=d[n%b]+s;n/=b)while n>0;s}

With '0' for empty string, 95 89 82 characters:

t=->n,b,s=''{d=*?0..?9,*?a..?z,*?A..?Z;(s=d[n%b]+s;n/=b)while n>0;s.empty?? ?0: s}

Ruby 1.9 - unfortunately only works up to base 36:

t=->n,b{n.to_s(b)}
\$\endgroup\$
  • 2
    \$\begingroup\$ You can replace the ]+['s with ,. \$\endgroup\$ – Nemo157 Feb 7 '11 at 0:14
1
\$\begingroup\$

Bash, 79 chars

f(){
dc<<<$2o$1p|perl -pe"y/A-Z/a-z/;s/ \d+/chr$&+($&<10?48:$&<36?87:29)/ge"
}
\$\endgroup\$
  • \$\begingroup\$ BC_BASE_MAX is documented as being 16. I don't know what miracle makes the output right on the sample input, but it outputs garbage (i.e. non alphanum characters) for most other bases. \$\endgroup\$ – J B Feb 6 '11 at 14:30
  • \$\begingroup\$ @J B: which bc are you using? GNU bc should work. sysconf(_SC_BC_BASE_MAX) returns 99 on my system, 16 is the minimum required. \$\endgroup\$ – ninjalj Feb 6 '11 at 14:37
  • \$\begingroup\$ @J B: also note that previous revisions where buggy, I had just skimmed at the question requirements. \$\endgroup\$ – ninjalj Feb 6 '11 at 14:39
  • \$\begingroup\$ bc 1.06. Now you mention it, I got the figure from the manpage, but misread it. 16 is the input base limit. The output base limit is 999. I did first try on an earlier version, let's see that again now. \$\endgroup\$ – J B Feb 6 '11 at 14:53
  • 1
    \$\begingroup\$ I think this one outputs uppercase letters for bases 11-16 instead of lower case. You can save a few at the base conversion by using dc instead of bc. \$\endgroup\$ – Nabb Feb 7 '11 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.