Skip to content

Commit cb584dd

Browse files
authored
Add largest-series-product (#328)
1 parent 3a8b203 commit cb584dd

File tree

8 files changed

+207
-0
lines changed

8 files changed

+207
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,14 @@
558558
"prerequisites": [],
559559
"difficulty": 5
560560
},
561+
{
562+
"slug": "largest-series-product",
563+
"name": "Largest Series Product",
564+
"uuid": "9ae397e6-da06-433a-b4dc-118c01b73a8a",
565+
"practices": [],
566+
"prerequisites": [],
567+
"difficulty": 5
568+
},
561569
{
562570
"slug": "nth-prime",
563571
"name": "Nth Prime",
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+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"largest-series-product.coffee"
8+
],
9+
"test": [
10+
"largest-series-product.spec.coffee"
11+
],
12+
"example": [
13+
".meta/example.coffee"
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class LargestSeriesProduct
2+
@largestProduct: (digits, span) ->
3+
if span < 0
4+
throw new Error 'span must not be negative'
5+
if span > digits.length
6+
throw new Error 'span must be smaller than string length'
7+
if digits.match(/[^0-9]/)
8+
throw new Error 'digits input must only contain digits'
9+
10+
max = 0
11+
for i in [0...digits.length - span + 1]
12+
product = 1
13+
for j in [0...span]
14+
product *= Number digits[i + j]
15+
if product > max
16+
max = product
17+
max
18+
19+
module.exports = LargestSeriesProduct
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class LargestSeriesProduct
2+
@largestProduct: (digits, span) ->
3+
4+
module.exports = LargestSeriesProduct
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
Lsp = require './largest-series-product'
2+
3+
describe 'Largest Series Product', ->
4+
it 'finds the largest product if span equals length', ->
5+
result = Lsp.largestProduct '29', 2
6+
expect(result).toEqual 18
7+
8+
xit 'can find the largest product of 2 with numbers in order', ->
9+
result = Lsp.largestProduct '0123456789', 2
10+
expect(result).toEqual 72
11+
12+
xit 'can find the largest product of 2', ->
13+
result = Lsp.largestProduct '576802143', 2
14+
expect(result).toEqual 48
15+
16+
xit 'can find the largest product of 3 with numbers in order', ->
17+
result = Lsp.largestProduct '0123456789', 3
18+
expect(result).toEqual 504
19+
20+
xit 'can find the largest product of 3', ->
21+
result = Lsp.largestProduct '1027839564', 3
22+
expect(result).toEqual 270
23+
24+
xit 'can find the largest product of 5 with numbers in order', ->
25+
result = Lsp.largestProduct '0123456789', 5
26+
expect(result).toEqual 15120
27+
28+
xit 'can get the largest product of a big number', ->
29+
result = Lsp.largestProduct '73167176531330624919225119674426574742355349194934', 6
30+
expect(result).toEqual 23520
31+
32+
xit 'reports zero if the only digits are zero', ->
33+
result = Lsp.largestProduct '0000', 2
34+
expect(result).toEqual 0
35+
36+
xit 'reports zero if all spans include zero', ->
37+
result = Lsp.largestProduct '99099', 3
38+
expect(result).toEqual 0
39+
40+
xit 'rejects span longer than string length', ->
41+
expect ->
42+
Lsp.largestProduct '123', 4
43+
.toThrow new Error 'span must be smaller than string length'
44+
45+
xit 'reports 1 for empty string and empty product (0 span)', ->
46+
result = Lsp.largestProduct '', 0
47+
expect(result).toEqual 1
48+
49+
xit 'reports 1 for nonempty string and empty product (0 span)', ->
50+
result = Lsp.largestProduct '123', 0
51+
expect(result).toEqual 1
52+
53+
xit 'rejects empty string and nonzero span', ->
54+
expect ->
55+
Lsp.largestProduct '', 1
56+
.toThrow new Error 'span must be smaller than string length'
57+
58+
xit 'rejects invalid character in digits', ->
59+
expect ->
60+
Lsp.largestProduct '1234a5', 2
61+
.toThrow new Error 'digits input must only contain digits'
62+
63+
xit 'rejects negative span', ->
64+
expect ->
65+
Lsp.largestProduct '12345', -1
66+
.toThrow new Error 'span must not be negative'

0 commit comments

Comments
 (0)