Skip to content

Instantly share code, notes, and snippets.

@planetis-m
Created April 7, 2019 21:09
Show Gist options
  • Save planetis-m/79503c6630dc6e216366650d3df4a762 to your computer and use it in GitHub Desktop.
Save planetis-m/79503c6630dc6e216366650d3df4a762 to your computer and use it in GitHub Desktop.
import strutils, algorithm
const wordlistFilename = "words.txt"
template with(f, fn, actions: untyped): untyped =
var f: File
if open(f, fn):
try:
actions
finally:
close(f)
else:
quit("cannot open: " & fn)
proc loadWords(): seq[string] =
echo "Loading word list from file..."
result = newSeqOfCap[string](83667)
with(inFile, wordlistFilename):
for line in lines(inFile):
result.add(line.strip().toLowerAscii())
echo " ", result.len, " words loaded."
proc main =
var wordlist = loadWords()
# isValidWord calls binarySearch, so wordList needs to be ordered.
sort(wordList)
let word = "binary"
doassert word in wordList
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment