Skip to content

Commit

Permalink
largest-series-product: Sync tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tasxatzial committed Jan 16, 2025
1 parent 8a23f63 commit 49ac011
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 46 deletions.
3 changes: 2 additions & 1 deletion exercises/practice/largest-series-product/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"mathias",
"petertseng",
"rubysolo",
"yurrriq"
"yurrriq",
"tasxatzial"
],
"files": {
"solution": [
Expand Down
30 changes: 18 additions & 12 deletions exercises/practice/largest-series-product/.meta/example.clj
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 exercises/practice/largest-series-product/.meta/generator.tpl
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~}}
18 changes: 15 additions & 3 deletions exercises/practice/largest-series-product/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[7c82f8b7-e347-48ee-8a22-f672323324d4]
description = "finds the largest product if span equals length"
Expand Down Expand Up @@ -48,3 +55,8 @@ description = "rejects invalid character in digits"

[5fe3c0e5-a945-49f2-b584-f0814b4dd1ef]
description = "rejects negative span"
include = false

[c859f34a-9bfe-4897-9c2f-6d7f8598e7f0]
description = "rejects negative span"
reimplements = "5fe3c0e5-a945-49f2-b584-f0814b4dd1ef"
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
)
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")))))

0 comments on commit 49ac011

Please sign in to comment.