Skip to content

Commit 75892ee

Browse files
authored
Add largest-series-product (exercism#201)
1 parent 01b7871 commit 75892ee

10 files changed

+278
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,14 @@
462462
"prerequisites": [],
463463
"difficulty": 3
464464
},
465+
{
466+
"slug": "largest-series-product",
467+
"name": "Largest Series Product",
468+
"uuid": "e3d2e2d5-bd70-42b8-8d61-871af9b16679",
469+
"practices": [],
470+
"prerequisites": [],
471+
"difficulty": 3
472+
},
465473
{
466474
"slug": "custom-set",
467475
"name": "Custom Set",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Instructions
2+
3+
Your task is to look for patterns in the long sequence of digits in the encrypted signal.
4+
5+
The technique you're going to use here is called the largest series product.
6+
7+
Let's define a few terms, first.
8+
9+
- **input**: the sequence of digits that you need to analyze
10+
- **series**: a sequence of adjacent digits (those that are next to each other) that is contained within the input
11+
- **span**: how many digits long each series is
12+
- **product**: what you get when you multiply numbers together
13+
14+
Let's work through an example, with the input `"63915"`.
15+
16+
- To form a series, take adjacent digits in the original input.
17+
- If you are working with a span of `3`, there will be three possible series:
18+
- `"639"`
19+
- `"391"`
20+
- `"915"`
21+
- Then we need to calculate the product of each series:
22+
- The product of the series `"639"` is 162 (`6 × 3 × 9 = 162`)
23+
- The product of the series `"391"` is 27 (`3 × 9 × 1 = 27`)
24+
- The product of the series `"915"` is 45 (`9 × 1 × 5 = 45`)
25+
- 162 is bigger than both 27 and 45, so the largest series product of `"63915"` is from the series `"639"`.
26+
So the answer is **162**.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Introduction
2+
3+
You work for a government agency that has intercepted a series of encrypted communication signals from a group of bank robbers.
4+
The signals contain a long sequence of digits.
5+
Your team needs to use various digital signal processing techniques to analyze the signals and identify any patterns that may indicate the planning of a heist.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"glennj"
4+
],
5+
"files": {
6+
"solution": [
7+
"largest-series-product.wren"
8+
],
9+
"test": [
10+
"largest-series-product.spec.wren"
11+
],
12+
"example": [
13+
".meta/proof.ci.wren"
14+
]
15+
},
16+
"blurb": "Given a string of digits, calculate the largest product for a contiguous substring of digits of length n.",
17+
"source": "A variation on Problem 8 at Project Euler",
18+
"source_url": "https://projecteuler.net/problem=8"
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Series {
2+
construct new(series, span) {
3+
_span = span
4+
_digits = series.codePoints.map {|cp| cp - 48}.toList
5+
6+
if (_span < 0) {
7+
Fiber.abort("span must not be negative")
8+
}
9+
if (_span > series.count) {
10+
Fiber.abort("span must be smaller than string length")
11+
}
12+
if (_digits.any {|d| d < 0 || d > 9}) {
13+
Fiber.abort("digits input must only contain digits")
14+
}
15+
}
16+
17+
largestProduct {
18+
var indices = 0..(_digits.count - _span)
19+
var products = indices.map {|i| product_(_digits[i...(i + _span)])}
20+
return max_(products)
21+
}
22+
23+
product_(list) { list.reduce(1) {|prod, num| prod * num} }
24+
max_(list) { list.reduce(0) {|max, prod| max.max(prod)} }
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[7c82f8b7-e347-48ee-8a22-f672323324d4]
13+
description = "finds the largest product if span equals length"
14+
15+
[88523f65-21ba-4458-a76a-b4aaf6e4cb5e]
16+
description = "can find the largest product of 2 with numbers in order"
17+
18+
[f1376b48-1157-419d-92c2-1d7e36a70b8a]
19+
description = "can find the largest product of 2"
20+
21+
[46356a67-7e02-489e-8fea-321c2fa7b4a4]
22+
description = "can find the largest product of 3 with numbers in order"
23+
24+
[a2dcb54b-2b8f-4993-92dd-5ce56dece64a]
25+
description = "can find the largest product of 3"
26+
27+
[673210a3-33cd-4708-940b-c482d7a88f9d]
28+
description = "can find the largest product of 5 with numbers in order"
29+
30+
[02acd5a6-3bbf-46df-8282-8b313a80a7c9]
31+
description = "can get the largest product of a big number"
32+
33+
[76dcc407-21e9-424c-a98e-609f269622b5]
34+
description = "reports zero if the only digits are zero"
35+
36+
[6ef0df9f-52d4-4a5d-b210-f6fae5f20e19]
37+
description = "reports zero if all spans include zero"
38+
39+
[5d81aaf7-4f67-4125-bf33-11493cc7eab7]
40+
description = "rejects span longer than string length"
41+
42+
[06bc8b90-0c51-4c54-ac22-3ec3893a079e]
43+
description = "reports 1 for empty string and empty product (0 span)"
44+
45+
[3ec0d92e-f2e2-4090-a380-70afee02f4c0]
46+
description = "reports 1 for nonempty string and empty product (0 span)"
47+
48+
[6d96c691-4374-4404-80ee-2ea8f3613dd4]
49+
description = "rejects empty string and nonzero span"
50+
51+
[7a38f2d6-3c35-45f6-8d6f-12e6e32d4d74]
52+
description = "rejects invalid character in digits"
53+
54+
[5fe3c0e5-a945-49f2-b584-f0814b4dd1ef]
55+
description = "rejects negative span"
56+
include = false
57+
58+
[c859f34a-9bfe-4897-9c2f-6d7f8598e7f0]
59+
description = "rejects negative span"
60+
reimplements = "5fe3c0e5-a945-49f2-b584-f0814b4dd1ef"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Exercism
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import "./largest-series-product" for Series
2+
import "wren-testie/testie" for Testie, Expect
3+
4+
Testie.test("LargestSeriesProduct") { |do, skip|
5+
6+
do.test("finds the largest product if span equals length") {
7+
var actual = Series.new("29", 2).largestProduct
8+
var expected = 18
9+
Expect.value(actual).toEqual(expected)
10+
}
11+
12+
skip.test("can find the largest product of 2 with numbers in order") {
13+
var actual = Series.new("0123456789", 2).largestProduct
14+
var expected = 72
15+
Expect.value(actual).toEqual(expected)
16+
}
17+
18+
skip.test("can find the largest product of 2") {
19+
var actual = Series.new("576802143", 2).largestProduct
20+
var expected = 48
21+
Expect.value(actual).toEqual(expected)
22+
}
23+
24+
skip.test("can find the largest product of 3 with numbers in order") {
25+
var actual = Series.new("0123456789", 3).largestProduct
26+
var expected = 504
27+
Expect.value(actual).toEqual(expected)
28+
}
29+
30+
skip.test("can find the largest product of 3") {
31+
var actual = Series.new("1027839564", 3).largestProduct
32+
var expected = 270
33+
Expect.value(actual).toEqual(expected)
34+
}
35+
36+
skip.test("can find the largest product of 5 with numbers in order") {
37+
var actual = Series.new("0123456789", 5).largestProduct
38+
var expected = 15120
39+
Expect.value(actual).toEqual(expected)
40+
}
41+
42+
skip.test("can get the largest product of a big number") {
43+
var actual = Series.new("73167176531330624919225119674426574742355349194934", 6).largestProduct
44+
var expected = 23520
45+
Expect.value(actual).toEqual(expected)
46+
}
47+
48+
skip.test("reports zero if the only digits are zero") {
49+
var actual = Series.new("0000", 2).largestProduct
50+
var expected = 0
51+
Expect.value(actual).toEqual(expected)
52+
}
53+
54+
skip.test("reports zero if all spans include zero") {
55+
var actual = Series.new("99099", 3).largestProduct
56+
var expected = 0
57+
Expect.value(actual).toEqual(expected)
58+
}
59+
60+
skip.test("rejects span longer than string length") {
61+
Expect.that {
62+
Series.new("123", 4).largestProduct
63+
}.abortsWith("span must be smaller than string length")
64+
}
65+
66+
skip.test("reports 1 for empty string and empty product (0 span)") {
67+
var actual = Series.new("", 0).largestProduct
68+
var expected = 1
69+
Expect.value(actual).toEqual(expected)
70+
}
71+
72+
skip.test("reports 1 for nonempty string and empty product (0 span)") {
73+
var actual = Series.new("123", 0).largestProduct
74+
var expected = 1
75+
Expect.value(actual).toEqual(expected)
76+
}
77+
78+
skip.test("rejects empty string and nonzero span") {
79+
Expect.that {
80+
Series.new("", 1).largestProduct
81+
}.abortsWith("span must be smaller than string length")
82+
}
83+
84+
skip.test("rejects invalid character in digits") {
85+
Expect.that {
86+
Series.new("1234a5", 2).largestProduct
87+
}.abortsWith("digits input must only contain digits")
88+
}
89+
90+
skip.test("rejects negative span") {
91+
Expect.that {
92+
Series.new("12345", -1).largestProduct
93+
}.abortsWith("span must not be negative")
94+
}
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Series {
2+
construct new() {
3+
Fiber.abort("Remove this statement and implement this function")
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import "wren-package" for WrenPackage, Dependency
2+
import "os" for Process
3+
4+
class Package is WrenPackage {
5+
construct new() {}
6+
name { "exercism/largest-series-product" }
7+
dependencies {
8+
return [
9+
Dependency.new("wren-testie", "0.3.0", "https://github.com/joshgoebel/wren-testie.git")
10+
]
11+
}
12+
}
13+
14+
Package.new().default()

0 commit comments

Comments
 (0)