-
Notifications
You must be signed in to change notification settings - Fork 1
/
ch1p8_zero_matrix.clj
61 lines (49 loc) · 989 Bytes
/
ch1p8_zero_matrix.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env bb
(defn contains-zero? [xs]
(some zero? xs))
(defn zero-all [xs]
(mapv (fn [_] 0) xs))
(defn zerofy-row [xs]
(if (contains-zero? xs)
(zero-all xs)
xs))
(defn zero-matrix [matrix]
(let [zeroed-rows (map zerofy-row matrix)
zeroed-cols (->> (apply map vector matrix)
(map zerofy-row)
(apply map vector))]
(mapv (fn [r1 r2] (map min r1 r2)) zeroed-rows zeroed-cols)))
(require '[clojure.test :as t])
(t/deftest my-test
(t/are [expected matrix] (= expected (zero-matrix matrix))
[[1]]
[[1]]
[[1 2]
[3 4]]
[[1 2]
[3 4]]
[[1 2 3]
[4 5 6]
[7 8 9]]
[[1 2 3]
[4 5 6]
[7 8 9]]
[[1 2 0]
[4 5 0]
[0 0 0]]
[[1 2 3]
[4 5 6]
[7 8 0]]
[[0 0 0]
[4 0 6]
[7 0 9]]
[[1 0 3]
[4 5 6]
[7 8 9]]
[[1 0 0]
[0 0 0]
[0 0 0]]
[[1 2 3]
[4 0 6]
[7 8 0]]))
(t/run-tests *ns*)