From 5fbec89c4e0ca9e53a27689c4ad2a7a66d843748 Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Tue, 6 Apr 2021 03:11:33 +0000 Subject: [PATCH] Implement random starting alphabet. --- alphabetter.go | 21 +++++++++++++++------ readme.md | 3 ++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/alphabetter.go b/alphabetter.go index bfdbb53..4b65649 100644 --- a/alphabetter.go +++ b/alphabetter.go @@ -3,8 +3,10 @@ package main import ( "fmt" "log" + "math/rand" "os" "sort" + "time" ) var SPELLINGS = map[rune]string{ @@ -37,6 +39,7 @@ var SPELLINGS = map[rune]string{ } func main() { + rand.Seed(time.Now().UnixNano()) random := false infinite := false for i := 1; i < len(os.Args); i++ { @@ -51,7 +54,7 @@ func main() { for { var alphabet []rune if random { - // todo + 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'} } @@ -59,7 +62,7 @@ func main() { fmt.Println(string(prev)) i := 0 for { - alphabet = sort_alphabet(alphabet) + alphabet = sortAlphabet(alphabet) if string(alphabet) == prev { if infinite { fmt.Println(prev) @@ -82,7 +85,7 @@ func main() { // 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 { +func sortAlphabet(alphabet []rune) []rune { new_alphabet := make([]rune, len(alphabet)) copy(new_alphabet, alphabet) sort.Slice(new_alphabet, func(i, j int) bool { @@ -94,8 +97,8 @@ func sort_alphabet(alphabet []rune) []rune { 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])) + i_index = findIndex(alphabet, rune(i_spelling[k])) + j_index = findIndex(alphabet, rune(j_spelling[k])) if i_index != j_index { break @@ -106,7 +109,7 @@ func sort_alphabet(alphabet []rune) []rune { return new_alphabet } -func find_index(alphabet []rune, target rune) int { +func findIndex(alphabet []rune, target rune) int { for i, letter := range alphabet { if letter == target { return i @@ -115,3 +118,9 @@ func find_index(alphabet []rune, target rune) int { log.Panicf("Tried to find a letter that doesn't exist") 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 +} diff --git a/readme.md b/readme.md index 522d130..1bf4f5b 100644 --- a/readme.md +++ b/readme.md @@ -27,5 +27,6 @@ with nearly any version of golang installed. Then execute it with ./alphabetter ``` -To loop forever, run with `--infinite`. +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`.