Skip to content

Commit 12b5890

Browse files
committed
update q 82
1 parent fabef63 commit 12b5890

File tree

1 file changed

+63
-5
lines changed

1 file changed

+63
-5
lines changed

tutorial/src/tutorial/q82.clj

+63-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
(ns q82)
2-
32
(defn word-chain?
43
[w1 w2]
54
(let [ch1 (first w1)
65
ch2 (first w2)]
76
(cond
7+
(empty? w1)(empty? w2)
88
(> (count w1) (count w2)) (recur w2 w1)
99
(= ch1 ch2) (recur (rest w1) (rest w2))
1010
(= ch1 (second w2)) (= (rest w1) (drop 2 w2))
@@ -19,21 +19,79 @@
1919
(println (first col1) "cur:" cur)
2020
(if (empty? col1) false
2121
(if (word-chain? cur (first col1)) true
22-
(recur (next col))))))
22+
(recur (rest col1))))))
2323

2424
(defn word-chain [col]
2525
(loop [col1 col]
2626
(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))))
2985

3086
(= (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"})
3288
(= true (word-chain #{"hat" "coat" "dog" "cat" "oat" "cot" "hot" "hog"}))
3389

90+
3491
(drop 2 "abadc")
3592
(next "abc")
3693

94+
(word-chain? "hog" "hog")
3795

3896
(word-chain? "acd" "adcd")
3997
(word-chain? "adcd" "acd")

0 commit comments

Comments
 (0)