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

Post History

66%
+2 −0
Challenges Presumptuous base conversion

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 the max() letter in D conver...

posted 1y ago by __blackjack__‭  ·  edited 1y ago by __blackjack__‭

Answer
#3: Post edited by user avatar __blackjack__‭ · 2022-09-22T14:23:48Z (over 1 year ago)
Inserted crossed out former size.
  • # Python 3, 59 bytes
  • ```python
  • 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 the `max()` letter in `D` converted to `int()` 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.
  • 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 3, ~~60~~ 59 bytes
  • ```python
  • 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 the `max()` letter in `D` converted to `int()` 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.
  • 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.
#2: Post edited by user avatar __blackjack__‭ · 2022-09-22T10:04:30Z (over 1 year ago)
Saved one byte by replacing `x if c else y` by `c and x or y`
  • # Python 3, 60 bytes
  • ```python
  • D=input();m=int(max(D),16);print(int(D,m+1)if m else len(D))
  • ```
  • Quite straight forward implementation.
  • * Input string is assigned to `D`.
  • * `m` is the the `max()` letter in `D` converted to `int()` 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: print the input parsed as integer with base `m+1`.
  • * …zero: print the length of the input.
  • # Python 3, 59 bytes
  • ```python
  • 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 the `max()` letter in `D` converted to `int()` 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.
  • 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.
#1: Initial revision by user avatar __blackjack__‭ · 2022-09-21T15:47:09Z (over 1 year ago)
# Python 3, 60 bytes

```python
D=input();m=int(max(D),16);print(int(D,m+1)if m else len(D))
```

Quite straight forward implementation.

* Input string is assigned to `D`.
* `m` is the the `max()` letter in `D` converted to `int()` 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: print the input parsed as integer with base `m+1`.
  * …zero: print the length of the input.