Skip to content

Commit

Permalink
Add q82.. difficult, some external resources used
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkun committed Oct 17, 2024
1 parent 2d566c2 commit d2aca94
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/ex/q082.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
;; https://4clojure.oxal.org/#/problem/082

(ns ex.q082)

(defn ans
"Problem 82, Word Chains"
[words]
(letfn [(tail [w] (->> w rest (apply str)))
(lev
[a b]
(cond
(= 0 (count b)) (count a)
(= 0 (count a)) (count b)
(= (first a) (first b)) (lev (tail a) (tail b))
:else (+ 1 (min (lev (tail a) b) (lev a (tail b)) (lev (tail a) (tail b))))))
(link? [a b] (<= (lev a b) 1))
(chain
[[word :as curr-chain] remaining]
(if (empty? remaining)
[curr-chain]
(apply concat (keep #(when (or (nil? word) (link? word %))
(chain (cons % curr-chain) (disj remaining %))) remaining))))]
(boolean (some #(= % words) (map set (chain () words))))))
14 changes: 14 additions & 0 deletions test/ex/q082_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
;; https://4clojure.oxal.org/#/problem/082

(ns ex.q082-test
(:require [clojure.test :as t]
[ex.q082 :as sut]))

(t/deftest test-q82
(t/testing "Problem 82, Word Chains"
(t/is (= true (sut/ans #{"hat" "coat" "dog" "cat" "oat" "cot" "hot" "hog"})))
(t/is (= false (sut/ans #{"cot" "hot" "bat" "fat"})))
(t/is (= false (sut/ans #{"to" "top" "stop" "tops" "toss"})))
(t/is (= true (sut/ans #{"spout" "do" "pot" "pout" "spot" "dot"})))
(t/is (= true (sut/ans #{"share" "hares" "shares" "hare" "are"})))
(t/is (= false (sut/ans #{"share" "hares" "hare" "are"})))))

0 comments on commit d2aca94

Please sign in to comment.