File tree Expand file tree Collapse file tree 4 files changed +23
-15
lines changed
exercises/practice/largest-series-product Expand file tree Collapse file tree 4 files changed +23
-15
lines changed Original file line number Diff line number Diff line change 11(ns largest-series-product )
22
3- (defn check -input
3+ (defn validate -input
44 [n digits]
55 (cond
66 (neg? n) (throw (IllegalArgumentException. " span must not be negative" ))
7- (or (> n (count digits)) (and (pos? n) (empty? digits))) (throw (IllegalArgumentException. " span must be smaller than string length" ))
8- (and (pos? n) (empty? digits)) (throw (IllegalArgumentException. " span must be smaller than string length" ))
7+ (> n (count digits)) (throw (IllegalArgumentException. " span must not exceed string length" ))
98 (not-every? #(<= 0 % 9 ) digits) (throw (IllegalArgumentException. " digits input must only contain digits" ))
10- :else false ))
9+ :else true ))
1110
1211(defn largest-product
1312 [n s]
1413 (let [digits (map #(Character/digit ^char % 10 ) s)]
15- (or (check-input n digits)
16- (if (zero? n)
17- 1
18- (->> digits
19- (partition n 1 )
20- (map #(reduce * %))
21- (apply max))))))
14+ (do
15+ (validate-input n digits)
16+ (->> digits
17+ (partition n 1 )
18+ (map #(reduce * %))
19+ (apply max)))))
Original file line number Diff line number Diff line change @@ -38,6 +38,11 @@ description = "reports zero if all spans include zero"
3838
3939[5d81aaf7-4f67-4125-bf33-11493cc7eab7 ]
4040description = " rejects span longer than string length"
41+ include = false
42+
43+ [0ae1ce53-d9ba-41bb-827f-2fceb64f058b ]
44+ description = " rejects span longer than string length"
45+ reimplements = " 5d81aaf7-4f67-4125-bf33-11493cc7eab7"
4146
4247[06bc8b90-0c51-4c54-ac22-3ec3893a079e ]
4348description = " reports 1 for empty string and empty product (0 span)"
@@ -49,6 +54,11 @@ include = false
4954
5055[6d96c691-4374-4404-80ee-2ea8f3613dd4 ]
5156description = " rejects empty string and nonzero span"
57+ include = false
58+
59+ [6cf66098-a6af-4223-aab1-26aeeefc7402 ]
60+ description = " rejects empty string and nonzero span"
61+ reimplements = " 6d96c691-4374-4404-80ee-2ea8f3613dd4"
5262
5363[7a38f2d6-3c35-45f6-8d6f-12e6e32d4d74 ]
5464description = " rejects invalid character in digits"
Original file line number Diff line number Diff line change 11(ns largest-series-product )
22
33(defn largest-product
4- " Returns the largest product of consecutive digits in a series of length n from the string s"
5- [n s]
4+ " Returns the largest product of any consecutive digits of length span in the string s. "
5+ [span s]
66 ; ; function body
77 )
Original file line number Diff line number Diff line change 4040
4141(deftest largest-product_test_10
4242 (testing " rejects span longer than string length"
43- (is (thrown-with-msg? IllegalArgumentException #"^span must be smaller than string length$" (largest-series-product/largest-product 4 " 123" )))))
43+ (is (thrown-with-msg? IllegalArgumentException #"^span must not exceed string length$" (largest-series-product/largest-product 4 " 123" )))))
4444
4545(deftest largest-product_test_11
4646 (testing " rejects empty string and nonzero span"
47- (is (thrown-with-msg? IllegalArgumentException #"^span must be smaller than string length$" (largest-series-product/largest-product 1 " " )))))
47+ (is (thrown-with-msg? IllegalArgumentException #"^span must not exceed string length$" (largest-series-product/largest-product 1 " " )))))
4848
4949(deftest largest-product_test_12
5050 (testing " rejects invalid character in digits"
You can’t perform that action at this time.
0 commit comments