|
1 | 1 | (ns q82)
|
2 |
| - |
3 | 2 | (defn word-chain?
|
4 | 3 | [w1 w2]
|
5 | 4 | (let [ch1 (first w1)
|
6 | 5 | ch2 (first w2)]
|
7 | 6 | (cond
|
| 7 | + (empty? w1)(empty? w2) |
8 | 8 | (> (count w1) (count w2)) (recur w2 w1)
|
9 | 9 | (= ch1 ch2) (recur (rest w1) (rest w2))
|
10 | 10 | (= ch1 (second w2)) (= (rest w1) (drop 2 w2))
|
|
19 | 19 | (println (first col1) "cur:" cur)
|
20 | 20 | (if (empty? col1) false
|
21 | 21 | (if (word-chain? cur (first col1)) true
|
22 |
| - (recur (next col)))))) |
| 22 | + (recur (rest col1)))))) |
23 | 23 |
|
24 | 24 | (defn word-chain [col]
|
25 | 25 | (loop [col1 col]
|
26 | 26 | (println "col" col1 col)
|
27 |
| - (if (has-word-chain? (first col1) col) (recur (rest col1)) |
28 |
| - false))) |
| 27 | + (if (empty? col1) true |
| 28 | + (if (has-word-chain? (first col1) col) (recur (rest col1)) |
| 29 | + false)))) |
| 30 | +(defn word-chain? |
| 31 | + [w1 w2] |
| 32 | + (let [ch1 (first w1) |
| 33 | + ch2 (first w2)] |
| 34 | + (cond |
| 35 | + (empty? w1)(empty? w2) |
| 36 | + (> (count w1) (count w2)) (recur w2 w1) |
| 37 | + (= ch1 ch2) (recur (rest w1) (rest w2)) |
| 38 | + (= ch1 (second w2)) (= (rest w1) (drop 2 w2)) |
| 39 | + (= ch2 (second w1)) (= (rest w2) (drop 2 w1)) |
| 40 | + :else (= (rest w1) (rest w2))))) |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +(defn has-word-chain? |
| 45 | + [cur col] |
| 46 | + (loop [col1 col] |
| 47 | + (println (first col1) "cur:" cur) |
| 48 | + (if (empty? col1) false |
| 49 | + (if ((fn [w1 w2] |
| 50 | + (let [ch1 (first w1) |
| 51 | + ch2 (first w2)] |
| 52 | + (cond |
| 53 | + (empty? w1)(empty? w2) |
| 54 | + (> (count w1) (count w2)) (recur w2 w1) |
| 55 | + (= ch1 ch2) (recur (rest w1) (rest w2)) |
| 56 | + (= ch1 (second w2)) (= (rest w1) (drop 2 w2)) |
| 57 | + (= ch2 (second w1)) (= (rest w2) (drop 2 w1)) |
| 58 | + :else (= (rest w1) (rest w2))))) |
| 59 | + cur (first col1)) true |
| 60 | + (recur (rest col1)))))) |
| 61 | + |
| 62 | +(defn word-chain [c] |
| 63 | + (loop [col1 c] |
| 64 | + (println "col" col1 c) |
| 65 | + (if (empty? col1) true |
| 66 | + (if ((fn [cur col] |
| 67 | + (loop [col1 col] |
| 68 | + (println (first col1) "cur:" cur) |
| 69 | + (if (empty? col1) false |
| 70 | + (if ((fn [w1 w2] |
| 71 | + (let [ch1 (first w1) |
| 72 | + ch2 (first w2)] |
| 73 | + (cond |
| 74 | + (empty? w1)(empty? w2) |
| 75 | + (> (count w1) (count w2)) (recur w2 w1) |
| 76 | + (= ch1 ch2) (recur (rest w1) (rest w2)) |
| 77 | + (= ch1 (second w2)) (= (rest w1) (drop 2 w2)) |
| 78 | + (= ch2 (second w1)) (= (rest w2) (drop 2 w1)) |
| 79 | + :else (= (rest w1) (rest w2))))) |
| 80 | + cur (first col1)) true |
| 81 | + (recur (rest col1)))))) |
| 82 | + (first col1) c) |
| 83 | + (recur (rest col1)) |
| 84 | + false)))) |
29 | 85 |
|
30 | 86 | (= (next "cd") (drop 2 "dcd"))
|
31 |
| -(has-word-chain? "hat" #{"hat" "coat" "dog" "cat" "oat" "cot" "hot" "hog"}) |
| 87 | +(has-word-chain? "hog" #{"hat" "coat" "dog" "cat" "oat" "cot" "hot" "hog"}) |
32 | 88 | (= true (word-chain #{"hat" "coat" "dog" "cat" "oat" "cot" "hot" "hog"}))
|
33 | 89 |
|
| 90 | + |
34 | 91 | (drop 2 "abadc")
|
35 | 92 | (next "abc")
|
36 | 93 |
|
| 94 | +(word-chain? "hog" "hog") |
37 | 95 |
|
38 | 96 | (word-chain? "acd" "adcd")
|
39 | 97 | (word-chain? "adcd" "acd")
|
|
0 commit comments