Created
December 20, 2020 12:10
-
-
Save scrouthtv/a411a90576699138f64ddc95f65d2477 to your computer and use it in GitHub Desktop.
Search a word based on given letters in any orders
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
package main | |
import "fmt" | |
import "bufio" | |
import "os" | |
import "sort" | |
import "strings" | |
func doSort(rs []rune) { | |
sort.SliceStable(rs, func(i int, j int) bool { | |
return rs[i] < rs[j] | |
}) | |
} | |
func check(line string) bool { | |
if len([]rune(line)) != reqlength { | |
return false | |
} | |
var iwant []rune = letters | |
doSort(iwant) | |
var ihave []rune = []rune(strings.ToLower(line)) | |
doSort(ihave) | |
var i, j int | |
j = 0 | |
for i = 0; i < len(iwant); i++ { | |
for letters[i] != ihave[j] { | |
j++ | |
if j == len(ihave) { | |
return false | |
} | |
} | |
j++ | |
} | |
return true | |
} | |
const reqlength int = 24 | |
var letters []rune = []rune{ | |
'e', 'k', 's', 'g', 'h', 'h', 'r', 'r', | |
'w', 's', 'u', 'r', 'o', 'c', 'e', 'ö', | |
't', 'e', 'e', 'r', | |
} | |
/*var letters []rune = []rune{ | |
'ö', 'f', 'b', 'b', | |
}*/ | |
func main() { | |
var f *os.File | |
var err error | |
f, err = os.Open("german.dic") | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
doSort(letters) | |
var rdr *bufio.Scanner = bufio.NewScanner(f) | |
for rdr.Scan() { | |
if check(rdr.Text()) { | |
fmt.Println(rdr.Text()) | |
} | |
} | |
f.Close() | |
} |
With 'n' words in the wordfile and 'm' required letters, runtime is at most
n * m log m
given that SliceStable
only takes
m log m
which it does not
Convert ISO to UTF8 using
~ iconv -i ISO-8859-1 -t UTF-8 german.dic > german8.dic
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also need https://sourceforge.net/projects/germandict/files/german.7z/download