Presumptuous base conversion
Take an input string representing a number and convert it to decimal (base 10). However, the base of the input is not specified. Assume the input is in the smallest base for which its digits are valid.
Input
- The input consists only of characters from 0123456789ABCDEF where A to F represent the decimal numbers 10 to 15
- The answerer may choose to take lower case input instead
- The input will always contain at least one character (it will never be empty)
- The input will sometimes have one or more leading zeroes
Output
- If the highest digit in the input is represented by N, then the input is to be treated as being in base N+1
- For this challenge, the letters A to F count as "digits". So if the highest digit in the input is B, which represents 11, then the input is in base 12
- In particular, if the highest digit in the input is 0, then the input is to be treated as being in base 1 (unary), so the output is the number of digits in the input
- Apart from inputs in base 1 (unary), leading zeroes make no difference to the output. Both "11" and "000011" lead to output "3"
Examples
- If the input is 453 then the highest digit is 5, so the input is in base 6. The output is 177, calculated as 4*6*6 + 5*6 + 3
- If the input is 000 then the highest digit is 0 so the input is in base 1 (unary). The output is 3, which is simply the length of the unary input
Test cases
Test cases are in the form input : output
0 : 1
1 : 1
9 : 9
A : 10
E : 14
F : 15
00 : 2
01 : 1
10 : 2
11 : 3
02 : 2
20 : 6
22 : 8
65 : 47
99 : 99
AA : 120
B8 : 140
000 : 3
00F : 15
0D0 : 182
C00 : 2028
123 : 27
ABC : 1845
777 : 511
453 : 177
Explanations in answers are optional, but I'm more likely to upvote answers that have one.
Python, 45 bytes I am not a …
2y ago
Python 3, 60 59 bytes ```py …
2y ago
Japt, 13 bytes Takes input …
2y ago
JavaScript, 73 71 63 53 52 byt …
2y ago
[C (gcc)], 117 bytes …
2y ago
5 answers
Python 3, 60 59 bytes
D=input();m=int(max(D),16);print(m and int(D,m+1)or len(D))
Quite straight forward implementation.
- Input string is assigned to
D
. -
m
is the themax()
letter inD
converted toint()
with base 16.max()
works because Python strings/characters are compared as Unicode codepoints and the letters "A"-"F" or "a"-"f" have a higher value than the characters for the decimal digits "0"-"9". - Depending on
m
being…- …not zero/”truthy”: print the input parsed as integer with base
m+1
. - …zero/”falsy”: print the length of the input.
- …not zero/”truthy”: print the input parsed as integer with base
The last part uses the fact that and
and or
evaluate to the first or the second operand instead of boolean values in Python. There's one pitfall with this trick: it doesn't work if the second argument to and
can be ”falsy” if the first is ”truthy”, but this can't happen here because there is no possible input that is a) not unary and b) zero at the same time, because to be non-unary under the given assignment there must be at least one non-zero digit in it, and then it can't have the value 0 any more.
Python, 45 bytes
I am not a Python guy at all, so I'm quite proud of this. I'm sure, though, there's something simple I could be doing to save myself a few bytes!
lambda n:int(n,int(max("1"+n),16)+1)or len(n)
JavaScript, 73 71 63 53 52 bytes
Well, this ain't pretty at all! Will need to take another pass over it to try to improve upon it, maybe with recursion.
Yeah, I was completely overthinking this one!
s=>parseInt(s,`0x`+[...s].sort().pop()-~0)||s.length
Try it online! (Includes all test cases)
0 comment threads
C (gcc), 117 bytes
b,r;f(char*s){char*p=s;for(;*p;p++)*p-=47+7*(*p>57),b=b<*p?*p:b;if(b<2)return p-s;for(;*s;s++)r*=b,r+=*s-1;return r;}
0 comment threads