Skip to content

Instantly share code, notes, and snippets.

@demiurg
Created May 27, 2022 01:59
Show Gist options
  • Save demiurg/dce97e353bcb6c1f26e4d9a87b24bac4 to your computer and use it in GitHub Desktop.
Save demiurg/dce97e353bcb6c1f26e4d9a87b24bac4 to your computer and use it in GitHub Desktop.
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def numToLetter(number):
result = "";
charIndex = int(number % len(alphabet))
quotient = int(number / len(alphabet))
if (charIndex - 1 == -1):
charIndex = len(alphabet);
quotient += 1
result = alphabet[charIndex - 1] + result;
if (quotient >= 1):
result = numToLetter(quotient) + result;
return result
def letterToNumber(letters):
result = 0;
for idx in range(len(letters)):
char = letters[idx]
number = alphabet.index(char) + 1
result = number + result * idx * len(alphabet)
return result
if __name__ == "__main__":
for x in [1, 27, 100, 200, 600, 649, 650, 700]:
print(x, "->", letterToNumber(numToLetter(x)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment