Created
May 13, 2018 17:18
-
-
Save msszczep/2c857d598aeff81d73b94cf9f8da2cd1 to your computer and use it in GitHub Desktop.
Wordbox in Clojure
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
(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