Skip to content

Commit f7c6040

Browse files
authored
Add practice exercise: largest-series-product (#191)
1 parent 915c9bd commit f7c6040

File tree

11 files changed

+305
-7
lines changed

11 files changed

+305
-7
lines changed

config.json

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,14 @@
362362
"prerequisites": [],
363363
"difficulty": 8
364364
},
365+
{
366+
"slug": "largest-series-product",
367+
"name": "Largest Series Product",
368+
"uuid": "66555259-8a28-428a-bca8-c0b06c889297",
369+
"practices": [],
370+
"prerequisites": [],
371+
"difficulty": 8
372+
},
365373
{
366374
"slug": "luhn",
367375
"name": "Luhn",
@@ -441,19 +449,19 @@
441449
"practices": [],
442450
"prerequisites": [],
443451
"difficulty": 8
444-
},
452+
},
445453
{
446-
"slug": "saddle-points",
447-
"name": "Saddle Points",
448-
"uuid": "d050e56b-9f86-416c-baa3-10a98f47b944",
454+
"slug": "run-length-encoding",
455+
"name": "Run-Length Encoding",
456+
"uuid": "065feae7-f6fc-46b9-a226-d3d9df9d192b",
449457
"practices": [],
450458
"prerequisites": [],
451459
"difficulty": 8
452460
},
453461
{
454-
"slug": "run-length-encoding",
455-
"name": "Run-Length Encoding",
456-
"uuid": "065feae7-f6fc-46b9-a226-d3d9df9d192b",
462+
"slug": "saddle-points",
463+
"name": "Saddle Points",
464+
"uuid": "d050e56b-9f86-416c-baa3-10a98f47b944",
457465
"practices": [],
458466
"prerequisites": [],
459467
"difficulty": 8
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"jimmytty"
4+
],
5+
"files": {
6+
"solution": [
7+
"largest-series-product.sql"
8+
],
9+
"test": [
10+
"largest-series-product_test.sql"
11+
],
12+
"example": [
13+
".meta/example.sql"
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+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
UPDATE "largest-series-product"
2+
SET error = 'span must not exceed string length'
3+
WHERE LENGTH(digits) < span
4+
;
5+
UPDATE "largest-series-product"
6+
SET error = 'digits input must only contain digits'
7+
WHERE GLOB('*[^0-9]*', digits)
8+
;
9+
UPDATE "largest-series-product"
10+
SET error = 'span must not be negative'
11+
WHERE span < 0
12+
;
13+
14+
UPDATE "largest-series-product"
15+
SET result = 1
16+
WHERE span = 0
17+
;
18+
19+
UPDATE "largest-series-product"
20+
SET result = (
21+
WITH RECURSIVE split_series (string, serie, product) AS (
22+
VALUES (digits, NULL, NULL)
23+
UNION ALL
24+
SELECT SUBSTR(string, 2),
25+
SUBSTR(string, 1, span),
26+
(WITH RECURSIVE split_digits (serie, digit) AS (
27+
VALUES (SUBSTR(string, 1, span), NULL)
28+
UNION ALL
29+
SELECT SUBSTR(serie, 2), SUBSTR(serie, 1, 1) * 1
30+
FROM split_digits
31+
WHERE serie <> ''
32+
)
33+
SELECT IIF(
34+
(SELECT 1 FROM split_digits WHERE digit = 0),
35+
0, EXP(SUM(LN(digit)))) AS product
36+
FROM split_digits
37+
WHERE digit NOTNULL
38+
)
39+
FROM split_series
40+
WHERE string <> ''
41+
)
42+
SELECT ROUND(MAX(product)) FROM split_series WHERE LENGTH(serie) = span
43+
)
44+
WHERE error ISNULL
45+
AND result ISNULL
46+
;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
include = false
42+
43+
[0ae1ce53-d9ba-41bb-827f-2fceb64f058b]
44+
description = "rejects span longer than string length"
45+
reimplements = "5d81aaf7-4f67-4125-bf33-11493cc7eab7"
46+
47+
[06bc8b90-0c51-4c54-ac22-3ec3893a079e]
48+
description = "reports 1 for empty string and empty product (0 span)"
49+
50+
[3ec0d92e-f2e2-4090-a380-70afee02f4c0]
51+
description = "reports 1 for nonempty string and empty product (0 span)"
52+
53+
[6d96c691-4374-4404-80ee-2ea8f3613dd4]
54+
description = "rejects empty string and nonzero span"
55+
include = false
56+
57+
[6cf66098-a6af-4223-aab1-26aeeefc7402]
58+
description = "rejects empty string and nonzero span"
59+
reimplements = "6d96c691-4374-4404-80ee-2ea8f3613dd4"
60+
61+
[7a38f2d6-3c35-45f6-8d6f-12e6e32d4d74]
62+
description = "rejects invalid character in digits"
63+
64+
[5fe3c0e5-a945-49f2-b584-f0814b4dd1ef]
65+
description = "rejects negative span"
66+
include = false
67+
68+
[c859f34a-9bfe-4897-9c2f-6d7f8598e7f0]
69+
description = "rejects negative span"
70+
reimplements = "5fe3c0e5-a945-49f2-b584-f0814b4dd1ef"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
DROP TABLE IF EXISTS "largest-series-product";
2+
CREATE TABLE "largest-series-product" (
3+
digits TEXT NOT NULL,
4+
span INTEGER NOT NULL,
5+
result INTEGER ,
6+
error TEXT
7+
);
8+
9+
.mode csv
10+
.import ./data.csv "largest-series-product"
11+
12+
UPDATE "largest-series-product" SET result = NULL, error = NULL;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
DROP TABLE IF EXISTS tests;
2+
CREATE TABLE IF NOT EXISTS tests (
3+
-- uuid and description are taken from the test.toml file
4+
uuid TEXT PRIMARY KEY,
5+
description TEXT NOT NULL,
6+
-- The following section is needed by the online test-runner
7+
status TEXT DEFAULT 'fail',
8+
message TEXT,
9+
output TEXT,
10+
test_code TEXT,
11+
task_id INTEGER DEFAULT NULL,
12+
-- Here are columns for the actual tests
13+
digits TEXT NOT NULL,
14+
span INTEGER NOT NULL,
15+
expected_result INTEGER,
16+
expected_error TEXT
17+
);
18+
19+
INSERT INTO tests (uuid, description, digits, span, expected_result, expected_error)
20+
VALUES
21+
('7c82f8b7-e347-48ee-8a22-f672323324d4', 'finds the largest product if span equals length', '29', 2, 18, NULL),
22+
('88523f65-21ba-4458-a76a-b4aaf6e4cb5e', 'can find the largest product of 2 with numbers in order', '0123456789', 2, 72, NULL),
23+
('f1376b48-1157-419d-92c2-1d7e36a70b8a', 'can find the largest product of 2', '576802143', 2, 48, NULL),
24+
('46356a67-7e02-489e-8fea-321c2fa7b4a4', 'can find the largest product of 3 with numbers in order', '0123456789', 3, 504, NULL),
25+
('a2dcb54b-2b8f-4993-92dd-5ce56dece64a', 'can find the largest product of 3', '1027839564', 3, 270, NULL),
26+
('673210a3-33cd-4708-940b-c482d7a88f9d', 'can find the largest product of 5 with numbers in order', '0123456789', 5, 15120, NULL),
27+
('02acd5a6-3bbf-46df-8282-8b313a80a7c9', 'can get the largest product of a big number', '73167176531330624919225119674426574742355349194934', 6, 23520, NULL),
28+
('76dcc407-21e9-424c-a98e-609f269622b5', 'reports zero if the only digits are zero', '0000', 2, 0, NULL),
29+
('6ef0df9f-52d4-4a5d-b210-f6fae5f20e19', 'reports zero if all spans include zero', '99099', 3, 0, NULL),
30+
('0ae1ce53-d9ba-41bb-827f-2fceb64f058b', 'rejects span longer than string length', '123', 4, NULL, 'span must not exceed string length'),
31+
('06bc8b90-0c51-4c54-ac22-3ec3893a079e', 'reports 1 for empty string and empty product (0 span)', '', 0, 1, NULL),
32+
('3ec0d92e-f2e2-4090-a380-70afee02f4c0', 'reports 1 for nonempty string and empty product (0 span)', '123', 0, 1, NULL),
33+
('6cf66098-a6af-4223-aab1-26aeeefc7402', 'rejects empty string and nonzero span', '', 1, NULL, 'span must not exceed string length'),
34+
('7a38f2d6-3c35-45f6-8d6f-12e6e32d4d74', 'rejects invalid character in digits', '1234a5', 2, NULL, 'digits input must only contain digits'),
35+
('c859f34a-9bfe-4897-9c2f-6d7f8598e7f0', 'rejects negative span', '12345', -1, NULL, 'span must not be negative');
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"29",2,,
2+
"0123456789",2,,
3+
"576802143",2,,
4+
"0123456789",3,,
5+
"1027839564",3,,
6+
"0123456789",5,,
7+
"73167176531330624919225119674426574742355349194934",6,,
8+
"0000",2,,
9+
"99099",3,,
10+
"123",4,,
11+
"",0,,
12+
"123",0,,
13+
"",1,,
14+
"1234a5",2,,
15+
"12345",-1,,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Schema:
2+
-- CREATE TABLE "largest-series-product" (
3+
-- digits TEXT NOT NULL,
4+
-- span INTEGER NOT NULL,
5+
-- result INTEGER ,
6+
-- error TEXT
7+
-- );
8+
--
9+
-- Task: update the largest-series-product table and set the result or the error columns bases on the digits and the span.

0 commit comments

Comments
 (0)