File tree 4 files changed +23
-15
lines changed
exercises/practice/largest-series-product
4 files changed +23
-15
lines changed Original file line number Diff line number Diff line change 1
1
(ns largest-series-product )
2
2
3
- (defn check -input
3
+ (defn validate -input
4
4
[n digits]
5
5
(cond
6
6
(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" ))
9
8
(not-every? #(<= 0 % 9 ) digits) (throw (IllegalArgumentException. " digits input must only contain digits" ))
10
- :else false ))
9
+ :else true ))
11
10
12
11
(defn largest-product
13
12
[n s]
14
13
(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"
38
38
39
39
[5d81aaf7-4f67-4125-bf33-11493cc7eab7 ]
40
40
description = " 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"
41
46
42
47
[06bc8b90-0c51-4c54-ac22-3ec3893a079e ]
43
48
description = " reports 1 for empty string and empty product (0 span)"
@@ -49,6 +54,11 @@ include = false
49
54
50
55
[6d96c691-4374-4404-80ee-2ea8f3613dd4 ]
51
56
description = " 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"
52
62
53
63
[7a38f2d6-3c35-45f6-8d6f-12e6e32d4d74 ]
54
64
description = " rejects invalid character in digits"
Original file line number Diff line number Diff line change 1
1
(ns largest-series-product )
2
2
3
3
(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]
6
6
; ; function body
7
7
)
Original file line number Diff line number Diff line change 40
40
41
41
(deftest largest-product_test_10
42
42
(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" )))))
44
44
45
45
(deftest largest-product_test_11
46
46
(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 " " )))))
48
48
49
49
(deftest largest-product_test_12
50
50
(testing " rejects invalid character in digits"
You can’t perform that action at this time.
0 commit comments