Skip to content

Instantly share code, notes, and snippets.

@dandrake
Last active February 25, 2023 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dandrake/a031d2f5716b5d80cc92b387f537f45f to your computer and use it in GitHub Desktop.
Save dandrake/a031d2f5716b5d80cc92b387f537f45f to your computer and use it in GitHub Desktop.
converting between "base ten" and "base T"
# 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