3
\$\begingroup\$

What your function must do:
be as short (in bytes) as possible
take 3 inputs that are strings: number , fromdigits, todigits
assume no digit is repeated in fromdigits or todigits
assume fromdigit and todigit contain only alphanumeric characters
assume number is a natural number
output (a string): number converted to frombase
examples:
convert("ff","0123456789abcdef","0123456789")="255"
convert("ee","fecdba9876543210","9876543210")="82"
example code (283 bytes, in python 3.4.1):

def convert(number,fromdigits,todigits):
    x=0
    for digit in str(number): 
        x = x*len(fromdigits)+fromdigits.index(digit)
    res=""
    while x>0:
        digit = x % len(todigits)
        res = todigits[digit] + res
        x //= len(todigits)
    return res
\$\endgroup\$
8
  • \$\begingroup\$ For a fastest code challenge, you need to say how you're going to measure the speed of the code. What inputs will be used? On what system will it be measured? \$\endgroup\$
    – xnor
    Commented Oct 1, 2014 at 3:18
  • \$\begingroup\$ be as short (in bytes) as possible \$\endgroup\$ Commented Oct 1, 2014 at 3:22
  • \$\begingroup\$ Then the tag you're looking for is code-golf. \$\endgroup\$
    – xnor
    Commented Oct 1, 2014 at 3:23
  • \$\begingroup\$ Almost there! You should specify the format of the input and output. You say "program" but define a function. The default is to allow both. Also, is there a limit to the characters that may be used to represent digits? \$\endgroup\$
    – xnor
    Commented Oct 1, 2014 at 3:26
  • 1
    \$\begingroup\$ The output of the convert("ff","0123456789abcdef","0123456789") call should be 255 instead of 256 \$\endgroup\$
    – Optimizer
    Commented Oct 1, 2014 at 7:26

3 Answers 3

1
\$\begingroup\$

Python - 101

To get started I basically golfed your example code...

def c(n,f,t,x=0,r=""):
 for d in n:x=x*len(f)+f.index(d)
 while x:l=len(t);r=t[x%l]+r;x//=l
 return r
\$\endgroup\$
1
\$\begingroup\$

APL (40)

Can't beat CJam anymore it seems.

{⎕IO←0⋄⍵⍵[N⊤⍨(⌊1+Z⍟N←(⍴⍺⍺)⊥⍺⍺⍳⍵)/Z←⍴⍵⍵]}

This takes the fromdigits and todigits values as its ⍺⍺ and ⍵⍵ arguments, and the number itself as the argument, i.e.:

      ('0123456789abcdef'{⎕IO←0⋄⍵⍵[N⊤⍨(⌊1+Z⍟N←(⍴⍺⍺)⊥⍺⍺⍳⍵)/Z←⍴⍵⍵]}'0123456789')'ff'
255
      ⍝ for ease of reading
      convert ← {⎕IO←0⋄⍵⍵[N⊤⍨(⌊1+Z⍟N←(⍴⍺⍺)⊥⍺⍺⍳⍵)/Z←⍴⍵⍵]}
      ('0123456789abcdef' convert '0123456789') 'ff'
255
      ('fedcba9876543210' convert '9876543210') 'ee'
82
      ('0123456789' convert '01') '254'
11111110

Explanation:

  • ⎕IO←0: set the index origin to 0. (APL arrays start at 1 by default and we don't need that here.)
  • Z←⍴⍵⍵: store the length of ⍵⍵ in Z. This is the base to convert to.
  • N←(⍴⍺⍺)⊥⍺⍺⍳⍵: look up the index () in ⍺⍺ of each value in . These are the digits in the base to convert from. Then decode () this from the input base, which is the length of ⍺⍺. Store this value in N.
  • ⌊1+Z⍟N: find the amount of digits required to represent value N in base Z.
  • (...)/Z: replicate Z that many times.
  • N⊤⍨: encode N into the output base.
  • ⍵⍵[...]: for each digit of that, find the corresponding character in ⍵⍵.
\$\endgroup\$
2
  • \$\begingroup\$ Why do you use 2 character variable names, plus, since those are unicodes, they are 4 bytes each \$\endgroup\$
    – Optimizer
    Commented Oct 1, 2014 at 10:23
  • \$\begingroup\$ @Optimizer: the APL charset fits in a byte just fine, and the 2-character variable names are just what APL calls them. and are function arguments, and ⍺⍺ and ⍵⍵ are the operator arguments. What this code is in fact doing is defining a 'convert' operator that takes two base strings on ⍺⍺ and ⍵⍵, which then gives a function that takes the number to be converted on . \$\endgroup\$
    – marinus
    Commented Oct 1, 2014 at 11:13
0
\$\begingroup\$

CJam, 20 bytes

rr:Ff#F,bAbsr:Tf#T,b

Input is like:

ee fecdba9876543210 9876543210

Output:

82

Try it online here

\$\endgroup\$

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