Last active
October 20, 2024 19:57
-
-
Save StevenACoffman/e282e29409c1b4ee861662744b85cf09 to your computer and use it in GitHub Desktop.
Four Digit room code to integer
This file contains 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
// https://go.dev/play/p/CxJf4Jf9rDZ | |
package main | |
import ( | |
"fmt" | |
"strings" | |
) | |
const ZZZZ = 18279 + 456975 | |
const AAAA = 18279 | |
func numberToEncodedLetter(number int64) string { | |
dictionary := getDictionary() | |
index := int(number) % len(dictionary) | |
quotient := number / int64(len(dictionary)) | |
var result string | |
if number <= int64(len(dictionary)) { | |
return numToLetter(int(number), dictionary) // Number is within single digit bounds of our encoding letter alphabet | |
} | |
if quotient >= 1 { | |
// This number was bigger than our dictionary, recursively perform this function until we're done | |
if index == 0 { | |
quotient-- // Accounts for the edge case of the last letter in the dictionary string | |
} | |
result = numberToEncodedLetter(quotient) | |
} | |
if index == 0 { | |
index = len(dictionary) // Accounts for the edge case of the final letter; avoids getting an empty string | |
} | |
return result + numToLetter(index, dictionary) | |
} | |
func numToLetter(number int, dictionary []string) string { | |
// Takes a letter between 0 and max letter length and returns the corresponding letter | |
if number > len(dictionary) || number < 0 { | |
return "" | |
} | |
if number == 0 { | |
return "" | |
} else { | |
return dictionary[number-1] | |
} | |
} | |
func getDictionary() []string { | |
return strings.Split("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "") | |
} | |
func main() { | |
// Example usage | |
fmt.Println(numberToEncodedLetter(475254)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment