Skip to content

Instantly share code, notes, and snippets.

@qwtel
Last active February 7, 2016 17:03
Show Gist options
  • Save qwtel/428d8dcced808de18464 to your computer and use it in GitHub Desktop.
Save qwtel/428d8dcced808de18464 to your computer and use it in GitHub Desktop.
find words in an input, lower-case only, brute force
(ns goog
(:require [clojure.string :as str]))
(def words-by-count (->> (slurp "/usr/share/dict/words" :encoding "ASCII")
str/split-lines
(map str/lower-case)
distinct
(map seq)
(group-by count)))
(def shortest-word 3)
(def longest-word (apply max (keys words-by-count)))
(defn find-words [input]
(for [i (range shortest-word (inc longest-word))
[pos part] (map-indexed vector (partition i 1 input))
word (get words-by-count i)
:when (= part word)
:let [str-word (apply str word)]]
[pos str-word]))
(def example-input
"ectintidaticavivestrianobsconthericarverantiontedistressinessupitousnesterworticshnomorolditicatomba")
(find-words example-input)
; => ([2 "tin"]
; [5 "tid"]
; [6 "ida"]
; [8 "ati"]
; [9 "tic"]
; [18 "tri"]
; [19 "ria"]
; [20 "ian"]
; [22 "nob"]
; [26 "con"]
; [28 "nth"]
; [29 "the"]
; [30 "her"]
; [32 "ric"]
; [34 "car"]
; [38 "era"]
; [39 "ran"]
; [40 "ant"]
; [43 "ion"]
; [46 "ted"]
; [48 "dis"]
; [49 "ist"]
; [53 "ess"]
; [54 "ssi"]
; [55 "sin"]
; [58 "ess"]
; [59 "ssu"]
; [60 "sup"]
; [62 "pit"]
; [63 "ito"]
; [64 "tou"]
; [75 "ort"]
; [77 "tic"]
; [84 "mor"]
; [87 "old"]
; [89 "dit"]
; [91 "tic"]
; [93 "cat"]
; [95 "tom"]
; [2 "tint"]
; [13 "vive"]
; [15 "vest"]
; [22 "nobs"]
; [31 "eric"]
; [37 "vera"]
; [39 "rant"]
; [40 "anti"]
; [50 "stre"]
; [55 "sine"]
; [57 "ness"]
; [68 "nest"]
; [74 "wort"]
; [84 "moro"]
; [94 "atom"]
; [95 "tomb"]
; [13 "vives"]
; [17 "stria"]
; [31 "erica"]
; [34 "carve"]
; [51 "tress"]
; [69 "ester"]
; [12 "avives"]
; [34 "carver"]
; [50 "stress"]
; [68 "nester"]
; [48 "distress"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment