Add 'stats mode' to demonstrate how frequent various numbers of iterations are.

This commit is contained in:
Anna Rose 2021-04-06 03:50:28 +00:00
parent 5fbec89c4e
commit ffb9d1a09f

View File

@ -38,22 +38,30 @@ var SPELLINGS = map[rune]string{
'Z': "ZEE",
}
var modeRandom = false
var modeInfinite = false
var modeStats = false
func main() {
rand.Seed(time.Now().UnixNano())
random := false
infinite := false
for i := 1; i < len(os.Args); i++ {
if os.Args[i] == "--random" {
random = true
modeRandom = true
}
if os.Args[i] == "--infinite" {
infinite = true
modeInfinite = true
}
if os.Args[i] == "--stats" {
modeStats = true
}
}
totalAlphabets := 0
// a count of how many alphabets stabilize in various quantities
stats := make(map[int]int)
for {
var alphabet []rune
if random {
if modeRandom {
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'}
@ -62,22 +70,32 @@ func main() {
fmt.Println(string(prev))
i := 0
for {
if i > 100 {
fmt.Println("Alphabet seems infinite.")
break
}
alphabet = sortAlphabet(alphabet)
if string(alphabet) == prev {
if infinite {
if modeInfinite {
fmt.Println(prev)
}
break
}
prev = string(alphabet)
if !infinite {
if !modeInfinite {
fmt.Println(prev)
}
i++
}
fmt.Println("Iterations:", i)
stats[i]++
totalAlphabets++
if !infinite {
if modeStats {
printStats(stats, totalAlphabets)
}
if !modeInfinite {
return
}
}
@ -124,3 +142,20 @@ func randomAlphabet() []rune {
rand.Shuffle(len(a), func(i, j int) { a[i], a[j] = a[j], a[i] })
return a
}
func printStats(counts map[int]int, total int) {
keys := make([]int, 0, len(counts))
for k, _ := range counts {
keys = append(keys, k)
}
sort.Ints(keys)
for _, k := range keys {
if k == 101 {
fmt.Printf("Infinite Loops: %.2f\n", float64(counts[k])/float64(total))
continue
}
fmt.Printf("%d: %.2f\n", k, float64(counts[k])/float64(total))
}
fmt.Println(counts)
}