Implement random starting alphabet.

This commit is contained in:
Anna Rose 2021-04-06 03:11:33 +00:00
parent 48d1a97acd
commit 5fbec89c4e
2 changed files with 17 additions and 7 deletions

View File

@ -3,8 +3,10 @@ package main
import ( import (
"fmt" "fmt"
"log" "log"
"math/rand"
"os" "os"
"sort" "sort"
"time"
) )
var SPELLINGS = map[rune]string{ var SPELLINGS = map[rune]string{
@ -37,6 +39,7 @@ var SPELLINGS = map[rune]string{
} }
func main() { func main() {
rand.Seed(time.Now().UnixNano())
random := false random := false
infinite := false infinite := false
for i := 1; i < len(os.Args); i++ { for i := 1; i < len(os.Args); i++ {
@ -51,7 +54,7 @@ func main() {
for { for {
var alphabet []rune var alphabet []rune
if random { if random {
// todo alphabet = randomAlphabet()
} else { } 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'} 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)) fmt.Println(string(prev))
i := 0 i := 0
for { for {
alphabet = sort_alphabet(alphabet) alphabet = sortAlphabet(alphabet)
if string(alphabet) == prev { if string(alphabet) == prev {
if infinite { if infinite {
fmt.Println(prev) fmt.Println(prev)
@ -82,7 +85,7 @@ func main() {
// 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 sort_alphabet(alphabet []rune) []rune { func sortAlphabet(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 {
@ -94,8 +97,8 @@ func sort_alphabet(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 = find_index(alphabet, rune(i_spelling[k])) i_index = findIndex(alphabet, rune(i_spelling[k]))
j_index = find_index(alphabet, rune(j_spelling[k])) j_index = findIndex(alphabet, rune(j_spelling[k]))
if i_index != j_index { if i_index != j_index {
break break
@ -106,7 +109,7 @@ func sort_alphabet(alphabet []rune) []rune {
return new_alphabet return new_alphabet
} }
func find_index(alphabet []rune, target rune) int { func findIndex(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
@ -115,3 +118,9 @@ func find_index(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

@ -27,5 +27,6 @@ with nearly any version of golang installed. Then execute it with
./alphabetter ./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`.