Skip to content

Instantly share code, notes, and snippets.

@willhbr
Last active August 29, 2015 14:16
Show Gist options
  • Save willhbr/2554849c3e95d56f8a43 to your computer and use it in GitHub Desktop.
Save willhbr/2554849c3e95d56f8a43 to your computer and use it in GitHub Desktop.
Go exhaustive language calculator
package main
import (
"fmt"
"math"
)
func size(depth int, alpha *[]string) int {
return int(math.Pow(float64(len(*alpha)), float64(depth)))
}
func all_strings(alpha []string, depth int) []string {
var out []string
if len(alpha) > 0 {
out = alpha
} else {
return []string{""}
}
for i := 1; i < depth; i++ {
new_out := make([]string, size(i + 1, &alpha))
currentpos := 0
for _, word := range out {
for _, char := range alpha {
new_out[currentpos] = word + char
currentpos += 1
}
}
out = new_out
}
return out
}
func main() {
alphabet := []string{"0", "1"}
fmt.Println(all_strings(alphabet, 4))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment