Skip to content

Commit cb7a03d

Browse files
Merge pull request #125 from keiravillekode/largest-series-product
Add largest-series-product exercise
2 parents 6c80ecc + 750d811 commit cb7a03d

File tree

14 files changed

+448
-0
lines changed

14 files changed

+448
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,14 @@
247247
"prerequisites": [],
248248
"difficulty": 4
249249
},
250+
{
251+
"slug": "largest-series-product",
252+
"name": "Largest Series Product",
253+
"uuid": "2534388c-96ef-45f7-a4e2-eb830dfb9c66",
254+
"practices": [],
255+
"prerequisites": [],
256+
"difficulty": 5
257+
},
250258
{
251259
"slug": "sieve",
252260
"name": "Sieve of Eratosthenes",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Instruction append
2+
3+
If the input is invalid, return `-1`.
4+
5+
## Reserved Memory
6+
7+
The buffer for the input string uses bytes 64-319 of linear memory.
8+
9+
The input string can be modified if desired.
Lines changed: 26 additions & 0 deletions
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**.
Lines changed: 5 additions & 0 deletions
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.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"root": true,
3+
"extends": "@exercism/eslint-config-javascript",
4+
"env": {
5+
"jest": true
6+
},
7+
"overrides": [
8+
{
9+
"files": [
10+
"*.spec.js"
11+
],
12+
"excludedFiles": [
13+
"custom.spec.js"
14+
],
15+
"extends": "@exercism/eslint-config-javascript/maintainers"
16+
}
17+
]
18+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"largest-series-product.wat"
8+
],
9+
"test": [
10+
"largest-series-product.spec.js"
11+
],
12+
"example": [
13+
".meta/proof.ci.wat"
14+
],
15+
"invalidator": [
16+
"package.json"
17+
]
18+
},
19+
"blurb": "Given a string of digits, calculate the largest product for a contiguous substring of digits of length n.",
20+
"source": "A variation on Problem 8 at Project Euler",
21+
"source_url": "https://projecteuler.net/problem=8",
22+
"custom": {
23+
"version.tests.compatibility": "jest-27",
24+
"flag.tests.task-per-describe": false,
25+
"flag.tests.may-run-long": false,
26+
"flag.tests.includes-optional": false
27+
}
28+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
(module
2+
(memory (export "mem") 1)
3+
4+
(global $ZERO i32 (i32.const 48))
5+
6+
;;
7+
;; Calculate the largest product for a contiguous substring of digits of length n.
8+
;;
9+
;; @param {i32} offset - offset of string in linear memory
10+
;; @param {i32} length - length of string in linear memory
11+
;;
12+
;; @returns {i32} largest digit product
13+
;;
14+
(func (export "largestProduct") (param $offset i32) (param $length i32) (param $span i32) (result i32)
15+
(local $product i32)
16+
(local $stop i32)
17+
(local $start i32)
18+
(local $result i32)
19+
(local $index i32)
20+
(local $digit i32)
21+
22+
(if (i32.or (i32.lt_s (local.get $span) (i32.const 0))
23+
(i32.gt_s (local.get $span) (local.get $length))) (then
24+
(return (i32.const -1))
25+
))
26+
27+
(if (i32.eqz (local.get $length)) (then
28+
(return (i32.const 1))
29+
))
30+
31+
(local.set $stop (i32.add (local.get $offset) (local.get $length)))
32+
33+
(local.set $index (local.get $offset))
34+
(loop
35+
(local.set $digit (i32.sub (i32.load8_u (local.get $index))
36+
(global.get $ZERO)))
37+
(if (i32.gt_u (local.get $digit) (i32.const 9)) (then
38+
;; $digit is not a valid digit.
39+
(return (i32.const -1))
40+
))
41+
(i32.store8 (local.get $index) (local.get $digit))
42+
43+
(local.set $index (i32.add (local.get $index) (i32.const 1)))
44+
(br_if 0 (i32.lt_u (local.get $index) (local.get $stop)))
45+
)
46+
47+
(if (i32.eqz (local.get $span)) (then
48+
(return (i32.const 1))
49+
))
50+
51+
(local.set $result (i32.const 0))
52+
53+
(local.set $start (i32.sub (local.get $stop) (local.get $span)))
54+
(loop $outer
55+
(local.set $product (i32.const 1))
56+
(local.set $index (local.get $start))
57+
(loop $inner
58+
(local.set $product (i32.mul (local.get $product)
59+
(i32.load8_u (local.get $index))))
60+
(local.set $index (i32.add (local.get $index) (i32.const 1)))
61+
(br_if $inner (i32.lt_u (local.get $index) (local.get $stop)))
62+
)
63+
(if (i32.lt_u (local.get $result) (local.get $product)) (then
64+
(local.set $result (local.get $product))
65+
))
66+
67+
(if (i32.eq (local.get $start) (local.get $offset)) (then
68+
(return (local.get $result))
69+
))
70+
71+
(local.set $start (i32.sub (local.get $start) (i32.const 1)))
72+
(local.set $stop (i32.sub (local.get $stop) (i32.const 1)))
73+
(br $outer)
74+
)
75+
(unreachable)
76+
)
77+
)
Lines changed: 60 additions & 0 deletions
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"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
audit=false
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 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.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default {
2+
presets: ["@exercism/babel-preset-javascript"],
3+
plugins: [],
4+
};

0 commit comments

Comments
 (0)