32
\$\begingroup\$

Here's a nice easy challenge:

Given a string that represents a number in an unknown base, determine the lowest possible base that number might be in. The string will only contain 0-9, a-z. If you like, you may choose to take uppercase letters instead of lowercase, but please specify this. You must output this lowest possible base in decimal.

Here is a more concrete example. If the input string was "01234", it is impossible for this number to be in binary, since 2, 3, and 4 are all undefined in binary. Similarly, this number cannot be in base 3, or base 4. Therefore, this number must be in base-5, or a higher base, so you should output '5'.

Your code must work for any base between base 1 (unary, all '0's) and base 36 ('0-9' and 'a-z').

You may take input and provide output in any reasonable format. Base-conversion builtins are allowed. As usual, standard loopholes apply, and the shortest answer in bytes is the winner!

Test IO:

#Input          #Output
00000       --> 1
123456      --> 7
ff          --> 16
4815162342  --> 9
42          --> 5
codegolf    --> 25
0123456789abcdefghijklmnopqrstuvwxyz    --> 36
\$\endgroup\$
17
  • 8
    \$\begingroup\$ Can I output in base 36? \$\endgroup\$
    – Leaky Nun
    Aug 23, 2016 at 4:42
  • 10
    \$\begingroup\$ @LeakyNun Geez, I hope not. \$\endgroup\$
    – Dennis
    Aug 23, 2016 at 5:22
  • 4
    \$\begingroup\$ @LeakyNun You must output this lowest possible base in decimal. \$\endgroup\$
    – DJMcMayhem
    Aug 23, 2016 at 6:09
  • 3
    \$\begingroup\$ @RohanJhunjhunwala If that's your languages closest equivalent to a string, I don't see why not. \$\endgroup\$
    – DJMcMayhem
    Aug 23, 2016 at 6:16
  • 3
    \$\begingroup\$ Usually unary is all 1s, and leading zeros are not standard for any positional-based numeric system. \$\endgroup\$
    – OrangeDog
    Aug 23, 2016 at 10:35

36 Answers 36

1
2
0
\$\begingroup\$

Perl, 30 27 bytes

Includes +1 for -p

Run with the input on STDIN, e.g.

base.pl <<< codegolf

base.pl:

#!/usr/bin/perl -p
\@F[unpack"W*"];$_=@F%39-9
\$\endgroup\$
0
\$\begingroup\$

LiveScript, 32 bytes

->1+parseInt (it/'')sort!pop!,36

A port of this answer in my favorite language that compile to JavaScript. If base~number operator worked with variables I could write ->1+36~(it/'')sort!pop! (23 bytes), but it conflicts with the function bind operator :/

\$\endgroup\$
0
\$\begingroup\$

PowerShell full program, 56 62 bytes

lowercase letters required.

([int]((read-host).ToCharArray()|sort|select -l 1)-8)%39
\$\endgroup\$
0
\$\begingroup\$

R, 59 57 bytes

Thanks to @Andreï-Kostyrka for two bytes.

max(which(c(0:9,letters)%in%strsplit(scan(,""),"")[[1]]))

scan(,'') takes the input and coerces it to a string. Then strsplit divides it into its individual characters. [[1]] is because that returns a list. %in% then checks which of the vector 0,1,2,...,8,9,a,b,c,...,x,y,z is in the input. which tells us the place in that vector of items in the input -- and max finds the biggest place -- which corresponds to the base.

\$\endgroup\$
1
  • \$\begingroup\$ Try removing the two extra spaces to save 2 bytes. \$\endgroup\$ Aug 29, 2016 at 10:12
0
\$\begingroup\$

05AB1E, 3 bytes

Hà>

Try it online or validate all test cases.

Explanation:

          # implicit input
H         # convert each digit to decimal
 à        # maximum
  >       # add 1
          # implicit output

\$\endgroup\$
0
\$\begingroup\$

Thunno 2 G, 4 bytes

36b⁺

Try it online!

Takes input in uppercase.

Explanation

36b⁺  # Implicit input
36b   # Convert each character from base-36
   ⁺  # Increment each integer
      # Implicit output of maximum
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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