Amazon

Friday, August 12, 2011

Programming Praxis: Word Breaks

Here's my solution to Programming Praxis: Word Breaks
;; word-breaks
(def dict #{"a" "apple" "applepie" "base" "baseball"
            "bat" "gift" "i" "pi" "pie" "twist"})

(defn permute [ss]
  (distinct 
   (mapcat identity 
           (let [cnt (count ss)
                 coll (take cnt (iterate rest ss))]
             (for [n (range 1 (inc cnt))]
               (mapcat (fn [x]
                         (map (fn [y] (apply str y))
                              (partition n x)))
                       coll))))))

(defn find-word-bounds [ss]
  (filter dict (permute ss)))
gist

No comments:

Post a Comment