Minimum base to count to a string
Python 2, 74 64 56 55 53 bytes
def f(x):a=ord(max(x))-54;a+=7*(a<8);print int(x,a),a
Call the function (i.e. f("HELLOWORLD")
) and outputs to stdout (809608041709942 33
)
This is case-sensitive, requiring upper case letters, and crashes for invalid input (e.g. "$^$%!(&£%)()
)
This code gets the max
letter in the string (z>x>y>...>b>a>9>8>...>2>1>0) and gets the largest base needed to represent it.
Change the 54
into a 76
to make it work for lowercase (but no longer upper case).
APL (24)
{(B⊥D-1),B←⌈/D←⍵⍳⍨⎕D,⎕A}
This is a function that takes a string and returns two values, the value and the base.
{(B⊥D-1),B←⌈/D←⍵⍳⍨⎕D,⎕A} 'DEADBEEF'
3735928559 16
Explanation:
⎕D,⎕A
: digits followed by lettersD←⍵⍳⍨
: store inD
the 1-based index of each character of ⍵ in the string of digits followed by lettersB←⌈/D
: store inB
the highest value inD
. (which is the base to use)(B⊥D-1)
: subtract 1 from each value inD
(making them the values of the digits), and decode them as a base-B
number.
Pure Bash, 48
for((b=37;$[--b]#$1;)){ :;}
echo $[$[++b]#$1],$b
Output
$ ./minbase.sh HELLOWORLD
./minbase.sh: line 1: ((: 32#HELLOWORLD: value too great for base (error token is "32#HELLOWORLD")
809608041709942,33
$ ./minbase.sh HELLOWORLD 2>/dev/null
809608041709942,33
$ ./minbase.sh 1 2>/dev/null
1,2
$
I'm taking advantage of this meta answer - specifically that warnings output to STDERR may be ignored.