Skip to content

Instantly share code, notes, and snippets.

@msszczep
Created May 13, 2018 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msszczep/2c857d598aeff81d73b94cf9f8da2cd1 to your computer and use it in GitHub Desktop.
Save msszczep/2c857d598aeff81d73b94cf9f8da2cd1 to your computer and use it in GitHub Desktop.
Wordbox in Clojure
(defn get-wordboxes [word]
"Given a word like 'cat', make all possible wordboxes in English. Here's an example wordbox:
CAT
O*E
GUN
Note that the words 'cog', 'ten' and 'gun' come from the word 'cat'.
The code below is too slow for words of four-letters-long or longer. I was hoping to make a full-fledged
web app from this code, but it would be too slow in most instances. I'm abandoning the effort and posting
the working code for reference.
The list of words used is the Official Scrabble Players Dictionary (English) 3rd edition, available as a text
file in my npr_sunday_puzzle_solutions repository in Github.
"
(let [word-arrays
(->> "/home/mitchells/Desktop/npr_sunday_puzzle_solutions/resources/ospd3.txt"
slurp
clojure.string/split-lines
(map (comp seq char-array))
set)
words-same-length (filter (comp (partial = (count word)) count)
word-arrays)
words-by-first-letter (group-by first words-same-length)]
(for [w1 (get words-by-first-letter (first word))
w2 (get words-by-first-letter (last word))
w3 (filter #(and (= (last w1) (first %))
(= (last w2) (last %)))
words-same-length)
:when (and (not= w1 w2)
(not= w1 w3)
(not= w2 w3)
(not= (apply str w1) word)
(not= (apply str w2) word)
(not= (apply str w3) word))]
[w1 w2 w3])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment