-
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a23f63
commit 49ac011
Showing
6 changed files
with
94 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,8 @@ | |
"mathias", | ||
"petertseng", | ||
"rubysolo", | ||
"yurrriq" | ||
"yurrriq", | ||
"tasxatzial" | ||
], | ||
"files": { | ||
"solution": [ | ||
|
30 changes: 18 additions & 12 deletions
30
exercises/practice/largest-series-product/.meta/example.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,21 @@ | ||
(ns largest-series-product) | ||
|
||
(defn- char->digit [c] | ||
{:pre [(Character/isDigit c)]} | ||
(Character/digit c 10)) | ||
|
||
(defn- digits [ds] (map char->digit ds)) | ||
|
||
(defn- slices [n ds] (partition n 1 (digits ds))) | ||
|
||
(defn largest-product [size ds] | ||
(defn check-input | ||
[n digits] | ||
(cond | ||
(zero? size) 1 | ||
(> size (count ds)) (throw (Exception. "Span must not exceed length.")) | ||
:else (apply max (map (partial apply *) (slices size ds))))) | ||
(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")) | ||
(not-every? #(<= 0 % 9) digits) (throw (IllegalArgumentException. "digits input must only contain digits")) | ||
:else false)) | ||
|
||
(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)))))) |
12 changes: 12 additions & 0 deletions
12
exercises/practice/largest-series-product/.meta/generator.tpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
(ns largest-series-product-test | ||
(:require [clojure.test :refer [deftest testing is]] | ||
largest-series-product)) | ||
{{#test_cases.largestProduct}} | ||
(deftest largest-product_test_{{idx}} | ||
(testing {{description}} | ||
{{~#if error}} | ||
(is (thrown-with-msg? IllegalArgumentException #{{error}} (largest-series-product/largest-product {{input.span}} {{input.digits}}))))) | ||
{{else}} | ||
(is (= {{expected}} (largest-series-product/largest-product {{input.span}} {{input.digits}}))))) | ||
{{/if~}} | ||
{{/test_cases.largestProduct~}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 5 additions & 3 deletions
8
exercises/practice/largest-series-product/src/largest_series_product.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
(ns largest-series-product) | ||
|
||
(defn largest-product [] ;; <- arglist goes here | ||
;; your code goes here | ||
) | ||
(defn largest-product | ||
"Calculates the largest product of consecutive digits in a series of length n from the string s" | ||
[n s] | ||
;; function body | ||
) |
69 changes: 42 additions & 27 deletions
69
exercises/practice/largest-series-product/test/largest_series_product_test.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,56 @@ | ||
(ns largest-series-product-test | ||
(:require [clojure.test :refer [deftest is testing]] | ||
[largest-series-product :as lsp])) | ||
(:require [clojure.test :refer [deftest testing is]] | ||
largest-series-product)) | ||
|
||
(deftest largest-series-tests | ||
(deftest largest-product_test_1 | ||
(testing "finds the largest product if span equals length" | ||
(is (= 18 (largest-series-product/largest-product 2 "29"))))) | ||
|
||
(deftest largest-product_test_2 | ||
(testing "can find the largest product of 2 with numbers in order" | ||
(is (= 72 (lsp/largest-product 2 "0123456789")))) | ||
(is (= 72 (largest-series-product/largest-product 2 "0123456789"))))) | ||
|
||
(deftest largest-product_test_3 | ||
(testing "can find the largest product of 2" | ||
(is (= 48 (lsp/largest-product 2 "576802143")))) | ||
(testing "finds the largest product if span equals length" | ||
(is (= 18 (lsp/largest-product 2 "29")))) | ||
(is (= 48 (largest-series-product/largest-product 2 "576802143"))))) | ||
(deftest largest-product_test_4 | ||
(testing "can find the largest product of 3 with numbers in order" | ||
(is (= 504 (lsp/largest-product 3 "0123456789")))) | ||
(is (= 504 (largest-series-product/largest-product 3 "0123456789"))))) | ||
|
||
(deftest largest-product_test_5 | ||
(testing "can find the largest product of 3" | ||
(is (= 270 (lsp/largest-product 3 "1027839564")))) | ||
(is (= 270 (largest-series-product/largest-product 3 "1027839564"))))) | ||
|
||
(deftest largest-product_test_6 | ||
(testing "can find the largest product of 5 with numbers in order" | ||
(is (= 15120 (lsp/largest-product 5 "0123456789")))) | ||
(is (= 15120 (largest-series-product/largest-product 5 "0123456789"))))) | ||
|
||
(deftest largest-product_test_7 | ||
(testing "can get the largest product of a big number" | ||
(is (= 23520 | ||
(let [ds "73167176531330624919225119674426574742355349194934"] | ||
(lsp/largest-product 6 ds))))) | ||
(testing "can get the largest product of a big number II" | ||
(is (= 28350 | ||
(let [ds "52677741234314237566414902593461595376319419139427"] | ||
(lsp/largest-product 6 ds))))) | ||
(testing "can get the largest product of a big number (Project Euler)" | ||
(is (= 23514624000 | ||
(let [ds "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"] | ||
(lsp/largest-product 13 ds))))) | ||
(is (= 23520 (largest-series-product/largest-product 6 "73167176531330624919225119674426574742355349194934"))))) | ||
|
||
(deftest largest-product_test_8 | ||
(testing "reports zero if the only digits are zero" | ||
(is (= 0 (lsp/largest-product 2 "0000")))) | ||
(is (= 0 (largest-series-product/largest-product 2 "0000"))))) | ||
|
||
(deftest largest-product_test_9 | ||
(testing "reports zero if all spans include zero" | ||
(is (= 0 (lsp/largest-product 3 "99099")))) | ||
(is (= 0 (largest-series-product/largest-product 3 "99099"))))) | ||
|
||
(deftest largest-product_test_10 | ||
(testing "rejects span longer than string length" | ||
(is (thrown? Throwable (lsp/largest-product 4 "123")))) | ||
(is (thrown-with-msg? IllegalArgumentException #"span must be smaller than string length" (largest-series-product/largest-product 4 "123"))))) | ||
|
||
(deftest largest-product_test_11 | ||
(testing "rejects empty string and nonzero span" | ||
(is (thrown? Throwable (lsp/largest-product 1 "")))) | ||
(is (thrown-with-msg? IllegalArgumentException #"span must be smaller than string length" (largest-series-product/largest-product 1 ""))))) | ||
|
||
(deftest largest-product_test_12 | ||
(testing "rejects invalid character in digits" | ||
(is (thrown? Throwable (lsp/largest-product 2 "1234a5")))) | ||
(is (thrown-with-msg? IllegalArgumentException #"digits input must only contain digits" (largest-series-product/largest-product 2 "1234a5"))))) | ||
|
||
(deftest largest-product_test_13 | ||
(testing "rejects negative span" | ||
(is (thrown? Throwable (lsp/largest-product -1 "12345"))))) | ||
(is (thrown-with-msg? IllegalArgumentException #"span must not be negative" (largest-series-product/largest-product -1 "12345"))))) | ||
|