Compare commits

..

No commits in common. "5fbec89c4e0ca9e53a27689c4ad2a7a66d843748" and "60d463a54632c22100e9620234a3a0c9ac9b58c7" have entirely different histories.

2 changed files with 37 additions and 81 deletions

View File

@ -3,89 +3,55 @@ package main
import ( import (
"fmt" "fmt"
"log" "log"
"math/rand"
"os"
"sort" "sort"
"time"
) )
var SPELLINGS = map[rune]string{ var SPELLINGS = map[rune]string{
'A': "AY", 'a': "ay",
'B': "BEE", 'b': "bee",
'C': "SEE", 'c': "see",
'D': "DEE", 'd': "dee",
'E': "EE", 'e': "ee",
'F': "EFF", 'f': "eff",
'G': "GEE", 'g': "gee",
'H': "AITCH", 'h': "aitch",
'I': "EYE", 'i': "eye",
'J': "JAY", 'j': "jay",
'K': "KAY", 'k': "kay",
'L': "ELL", 'l': "ell",
'M': "EMM", 'm': "emm",
'N': "ENN", 'n': "enn",
'O': "OH", 'o': "oh",
'P': "PEE", 'p': "pee",
'Q': "CUE", 'q': "cue",
'R': "ARR", 'r': "arr",
'S': "ESS", 's': "ess",
'T': "TEE", 't': "tee",
'U': "EWE", 'u': "ewe",
'V': "VEE", 'v': "vee",
'W': "DOUBLE EWE", 'w': "double ewe",
'X': "ECKS", 'x': "ecks",
'Y': "WHY", 'y': "why",
'Z': "ZEE", 'z': "zee",
} }
func main() { func main() {
rand.Seed(time.Now().UnixNano()) 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'}
random := false
infinite := false
for i := 1; i < len(os.Args); i++ {
if os.Args[i] == "--random" {
random = true
}
if os.Args[i] == "--infinite" {
infinite = true
}
}
for {
var alphabet []rune
if random {
alphabet = randomAlphabet()
} else {
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) prev := string(alphabet)
fmt.Println(string(prev)) fmt.Println(string(prev))
i := 0
for { for {
alphabet = sortAlphabet(alphabet) alphabet = sort_alphabet(alphabet)
if string(alphabet) == prev { if string(alphabet) == prev {
if infinite {
fmt.Println(prev)
}
break
}
prev = string(alphabet)
if !infinite {
fmt.Println(prev)
}
i++
}
fmt.Println("Iterations:", i)
if !infinite {
return return
} }
prev = string(alphabet)
fmt.Println(prev)
} }
} }
// sort the provided alphabet (a slice of runes) by spelling, using the alphabet itself // sort the provided alphabet (a slice of runes) by spelling, using the alphabet itself
// as the lexicographic ordering. Return the new alphabet. // as the lexicographic ordering. Return the new alphabet.
func sortAlphabet(alphabet []rune) []rune { func sort_alphabet(alphabet []rune) []rune {
new_alphabet := make([]rune, len(alphabet)) new_alphabet := make([]rune, len(alphabet))
copy(new_alphabet, alphabet) copy(new_alphabet, alphabet)
sort.Slice(new_alphabet, func(i, j int) bool { sort.Slice(new_alphabet, func(i, j int) bool {
@ -97,8 +63,8 @@ func sortAlphabet(alphabet []rune) []rune {
k := 0 k := 0
for k = 0; k < len(i_spelling) && k < len(j_spelling); k++ { for k = 0; k < len(i_spelling) && k < len(j_spelling); k++ {
i_index = findIndex(alphabet, rune(i_spelling[k])) i_index = find_index(alphabet, rune(i_spelling[k]))
j_index = findIndex(alphabet, rune(j_spelling[k])) j_index = find_index(alphabet, rune(j_spelling[k]))
if i_index != j_index { if i_index != j_index {
break break
@ -109,7 +75,7 @@ func sortAlphabet(alphabet []rune) []rune {
return new_alphabet return new_alphabet
} }
func findIndex(alphabet []rune, target rune) int { func find_index(alphabet []rune, target rune) int {
for i, letter := range alphabet { for i, letter := range alphabet {
if letter == target { if letter == target {
return i return i
@ -118,9 +84,3 @@ func findIndex(alphabet []rune, target rune) int {
log.Panicf("Tried to find a letter that doesn't exist") log.Panicf("Tried to find a letter that doesn't exist")
return -1 return -1
} }
func randomAlphabet() []rune {
a := []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'}
rand.Shuffle(len(a), func(i, j int) { a[i], a[j] = a[j], a[i] })
return a
}

View File

@ -26,7 +26,3 @@ with nearly any version of golang installed. Then execute it with
``` ```
./alphabetter ./alphabetter
``` ```
To loop forever, run with `--infinite`. While in infinite mode, only the starting alphabet and final alphabet are printed.
To start with a random alphabet (on each iteration), run with `--random`.