-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reads the valid words, creates a histogram for letters and ranks words.
- Loading branch information
0 parents
commit 323004e
Showing
4 changed files
with
2,437 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
var letters = []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"} | ||
var validWords = []string{} | ||
var dictionary = map[string]int{} | ||
var histogram = map[string]int{} | ||
var rankedWords PairList | ||
|
||
func main() { | ||
// Read the valid words | ||
fmt.Println("Reading words...") | ||
err := readValidWords() | ||
check(err) | ||
|
||
// Generate histogram | ||
fmt.Println("Building histogram...") | ||
buildHistogram() | ||
|
||
// Rank words based on frequency | ||
fmt.Println("Ranking words...") | ||
rankWords() | ||
|
||
fmt.Printf("%+v\n", histogram) | ||
for i := 0; i < 10; i++ { | ||
rank := rankedWords[i] | ||
fmt.Printf("%s: %d\n", rank.Key, rank.Value) | ||
} | ||
} | ||
|
||
func check(e error) { | ||
if e != nil { | ||
panic(e) | ||
} | ||
} | ||
|
||
func readValidWords() error { | ||
file, err := os.Open("./solutions.txt") | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
scanner := bufio.NewScanner(file) | ||
i := 0 | ||
for scanner.Scan() { | ||
word := scanner.Text() | ||
dictionary[word] = i | ||
validWords = append(validWords, word) | ||
i++ | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func buildHistogram() { | ||
histogram = make(map[string]int, len(letters)) | ||
for _, l := range letters { | ||
histogram[l] = 0 | ||
} | ||
|
||
// Loop through each word and check which unique letters are in the word | ||
for _, word := range validWords { | ||
chrs := strings.Split(word, "") | ||
|
||
// Loop through each char and update the histogram | ||
checkedChars := map[string]bool{} | ||
for _, chr := range chrs { | ||
// If we've not already processed this letter | ||
if _, ok := checkedChars[chr]; !ok { | ||
histogram[chr]++ | ||
checkedChars[chr] = true | ||
} | ||
} | ||
} | ||
} | ||
|
||
func rankWords() { | ||
rankedWords = make(PairList, len(validWords)) | ||
|
||
for _, word := range validWords { | ||
chrs := strings.Split(word, "") | ||
// First set the score based on the letters that exist | ||
checkedChars := map[string]bool{} | ||
score := 0 | ||
for _, chr := range chrs { | ||
if _, ok := checkedChars[chr]; !ok { | ||
score += histogram[chr] | ||
checkedChars[chr] = true | ||
} | ||
} | ||
|
||
// Add to the ranked list | ||
rankedWords = append(rankedWords, Pair{word, score}) | ||
} | ||
|
||
// Sort the | ||
sort.Sort(sort.Reverse(rankedWords)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package main | ||
|
||
type Pair struct { | ||
Key string | ||
Value int | ||
} | ||
|
||
type PairList []Pair | ||
|
||
func (p PairList) Len() int { return len(p) } | ||
func (p PairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] } | ||
func (p PairList) Less(i, j int) bool { return p[i].Value < p[j].Value } |
Oops, something went wrong.