Last active
February 25, 2023 14:25
-
-
Save dandrake/a031d2f5716b5d80cc92b387f537f45f to your computer and use it in GitHub Desktop.
converting between "base ten" and "base T"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python code accompanying https://ddrake.prose.sh/base_ten_without_zero | |
def T_to_zero(Tnum): | |
"""Convert a number (provided as a string) written with "T" notation | |
to a Python integer, which by default is displayed using the | |
ordinary positional notation with "0". | |
""" | |
T_digit_to_int = {str(i): i for i in range(1, 10)} | |
T_digit_to_int['T'] = 10 | |
return sum(10**p * T_digit_to_int[d] for p, d in enumerate(reversed(Tnum))) | |
def zero_to_T(x): | |
"""Convert a number (a positive Python int) into "T" notation.""" | |
if x == 0: | |
return "" | |
else: | |
r = x % 10 | |
r_ = r if r > 0 else 10 | |
y = (x - r_) // 10 | |
int_to_T_digit = {i: str(i) for i in range(1, 10)} | |
int_to_T_digit[0] = 'T' | |
return zero_to_T(y) + int_to_T_digit[r] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment