Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Comments on Presumptuous base conversion

Parent

Presumptuous base conversion

+3
−0

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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+3
−0

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)

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

2 comment threads

Moving the `f=\` into the header feels like cheating to me. This is two bytes longer to be actually u... (6 comments)
This gives 3 for input 10: should be 2. (2 comments)
Moving the `f=\` into the header feels like cheating to me. This is two bytes longer to be actually u...
__blackjack__‭ wrote about 2 years ago

Moving the f=\ into the header feels like cheating to me. This is two bytes longer to be actually usable/callable.

Shaggy‭ wrote about 2 years ago

It's an anonymous function, __blackjack__‭, just like we have in JavaScript .The f= would only need to be included if the function were recursive.

trichoplax‭ wrote about 2 years ago · edited about 2 years ago

This has been mentioned previously on Meta in Rules for function submissions if either of you want to vote/comment/answer in that discussion.

Current voting seems to suggest that anonymous functions are acceptable.

__blackjack__‭ wrote about 2 years ago

Shaggy‭ I know what that is, and it feels a bit useless to me here because it isn't called and can't be called from somewhere else because it has no name or other means to actually reference it in a call. So f= would have to be included to be callable at all.

Well, IMHO. As trichoplax‭ pointed out this seemed to be okay for the people who voted so far. So I'll upvote your solution. 🙂

trichoplax‭ wrote about 2 years ago

__blackjack__‭ if it affects your perspective, some languages allow passing an anonymous function as a function argument, or returning an anonymous function as a return value. This allows another part of the program to call the anonymous function without ever having to name it.

This is common with lambdas in python (for example, defined in place as an argument to something like map) and in Javascript Immediately Invoked Function Expressions are a popular pattern.

trichoplax‭ wrote about 2 years ago

Also, instead of

Current voting seems to suggest that anonymous functions are acceptable

I should have said

Voting seems to suggest that anonymous functions are currently acceptable

Meta is always open to further discussion, new answers, and new votes. Anything you disagree with, feel free to add your thoughts - decisions aren't necessarily permanent