alphabetter/alphabetter.go

87 lines
1.7 KiB
Go
Raw Normal View History

2021-04-05 23:20:35 +00:00
package main
import (
"fmt"
"log"
"sort"
)
var SPELLINGS = map[rune]string{
'a': "ay",
'b': "bee",
'c': "see",
'd': "dee",
'e': "ee",
'f': "eff",
'g': "gee",
'h': "aitch",
'i': "eye",
'j': "jay",
'k': "kay",
'l': "ell",
'm': "emm",
'n': "enn",
'o': "oh",
'p': "pee",
'q': "cue",
'r': "arr",
's': "ess",
't': "tee",
'u': "ewe",
'v': "vee",
'w': "double ewe",
'x': "ecks",
'y': "why",
'z': "zee",
}
func main() {
alphabet := []rune{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}
prev := string(alphabet)
fmt.Println(string(prev))
for {
alphabet = sort_alphabet(alphabet)
if string(alphabet) == prev {
return
}
prev = string(alphabet)
fmt.Println(prev)
}
}
// sort the provided alphabet (a slice of runes) by spelling, using the alphabet itself
// as the lexicographic ordering. Return the new alphabet.
func sort_alphabet(alphabet []rune) []rune {
new_alphabet := make([]rune, len(alphabet))
copy(new_alphabet, alphabet)
sort.Slice(new_alphabet, func(i, j int) bool {
i_spelling := SPELLINGS[new_alphabet[i]]
j_spelling := SPELLINGS[new_alphabet[j]]
i_index := -1
j_index := -1
k := 0
for k = 0; k < len(i_spelling) && k < len(j_spelling); k++ {
i_index = find_index(alphabet, rune(i_spelling[k]))
j_index = find_index(alphabet, rune(j_spelling[k]))
if i_index != j_index {
break
}
}
return i_index < j_index
})
return new_alphabet
}
func find_index(alphabet []rune, target rune) int {
for i, letter := range alphabet {
if letter == target {
return i
}
}
log.Panicf("Tried to find a letter that doesn't exist")
return -1
}