Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions exercises/practice/largest-series-product/.meta/example.clj
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
(ns largest-series-product)

(defn check-input
(defn validate-input
[n digits]
(cond
(neg? n) (throw (IllegalArgumentException. "span must not be negative"))
(or (> n (count digits)) (and (pos? n) (empty? digits))) (throw (IllegalArgumentException. "span must be smaller than string length"))
(and (pos? n) (empty? digits)) (throw (IllegalArgumentException. "span must be smaller than string length"))
(> n (count digits)) (throw (IllegalArgumentException. "span must not exceed string length"))
(not-every? #(<= 0 % 9) digits) (throw (IllegalArgumentException. "digits input must only contain digits"))
:else false))
:else true))

(defn largest-product
[n s]
(let [digits (map #(Character/digit ^char % 10) s)]
(or (check-input n digits)
(if (zero? n)
1
(->> digits
(partition n 1)
(map #(reduce * %))
(apply max))))))
(do
(validate-input n digits)
(->> digits
(partition n 1)
(map #(reduce * %))
(apply max)))))
10 changes: 10 additions & 0 deletions exercises/practice/largest-series-product/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ description = "reports zero if all spans include zero"

[5d81aaf7-4f67-4125-bf33-11493cc7eab7]
description = "rejects span longer than string length"
include = false

[0ae1ce53-d9ba-41bb-827f-2fceb64f058b]
description = "rejects span longer than string length"
reimplements = "5d81aaf7-4f67-4125-bf33-11493cc7eab7"

[06bc8b90-0c51-4c54-ac22-3ec3893a079e]
description = "reports 1 for empty string and empty product (0 span)"
Expand All @@ -49,6 +54,11 @@ include = false

[6d96c691-4374-4404-80ee-2ea8f3613dd4]
description = "rejects empty string and nonzero span"
include = false

[6cf66098-a6af-4223-aab1-26aeeefc7402]
description = "rejects empty string and nonzero span"
reimplements = "6d96c691-4374-4404-80ee-2ea8f3613dd4"

[7a38f2d6-3c35-45f6-8d6f-12e6e32d4d74]
description = "rejects invalid character in digits"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns largest-series-product)

(defn largest-product
"Returns the largest product of consecutive digits in a series of length n from the string s"
[n s]
"Returns the largest product of any consecutive digits of length span in the string s."
[span s]
;; function body
)
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@

(deftest largest-product_test_10
(testing "rejects span longer than string length"
(is (thrown-with-msg? IllegalArgumentException #"^span must be smaller than string length$" (largest-series-product/largest-product 4 "123")))))
(is (thrown-with-msg? IllegalArgumentException #"^span must not exceed string length$" (largest-series-product/largest-product 4 "123")))))

(deftest largest-product_test_11
(testing "rejects empty string and nonzero span"
(is (thrown-with-msg? IllegalArgumentException #"^span must be smaller than string length$" (largest-series-product/largest-product 1 "")))))
(is (thrown-with-msg? IllegalArgumentException #"^span must not exceed string length$" (largest-series-product/largest-product 1 "")))))

(deftest largest-product_test_12
(testing "rejects invalid character in digits"
Expand Down