Skip to content

Commit

Permalink
Add largest-series-product
Browse files Browse the repository at this point in the history
  • Loading branch information
glennj committed Jul 2, 2024
1 parent 01b7871 commit bb8417e
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "largest-series-product",
"name": "Largest Series Product",
"uuid": "e3d2e2d5-bd70-42b8-8d61-871af9b16679",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "custom-set",
"name": "Custom Set",
Expand Down
26 changes: 26 additions & 0 deletions exercises/practice/largest-series-product/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Instructions

Your task is to look for patterns in the long sequence of digits in the encrypted signal.

The technique you're going to use here is called the largest series product.

Let's define a few terms, first.

- **input**: the sequence of digits that you need to analyze
- **series**: a sequence of adjacent digits (those that are next to each other) that is contained within the input
- **span**: how many digits long each series is
- **product**: what you get when you multiply numbers together

Let's work through an example, with the input `"63915"`.

- To form a series, take adjacent digits in the original input.
- If you are working with a span of `3`, there will be three possible series:
- `"639"`
- `"391"`
- `"915"`
- Then we need to calculate the product of each series:
- The product of the series `"639"` is 162 (`6 × 3 × 9 = 162`)
- The product of the series `"391"` is 27 (`3 × 9 × 1 = 27`)
- The product of the series `"915"` is 45 (`9 × 1 × 5 = 45`)
- 162 is bigger than both 27 and 45, so the largest series product of `"63915"` is from the series `"639"`.
So the answer is **162**.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Introduction

You work for a government agency that has intercepted a series of encrypted communication signals from a group of bank robbers.
The signals contain a long sequence of digits.
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.
19 changes: 19 additions & 0 deletions exercises/practice/largest-series-product/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"glennj"
],
"files": {
"solution": [
"largest-series-product.wren"
],
"test": [
"largest-series-product.spec.wren"
],
"example": [
".meta/proof.ci.wren"
]
},
"blurb": "Given a string of digits, calculate the largest product for a contiguous substring of digits of length n.",
"source": "A variation on Problem 8 at Project Euler",
"source_url": "https://projecteuler.net/problem=8"
}
25 changes: 25 additions & 0 deletions exercises/practice/largest-series-product/.meta/proof.ci.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Series {
construct new(series, span) {
_span = span
_digits = series.codePoints.map {|cp| cp - 48}.toList

if (_span < 0) {
Fiber.abort("span must not be negative")
}
if (_span > series.count) {
Fiber.abort("span must be smaller than string length")
}
if (_digits.any {|d| d < 0 || d > 9}) {
Fiber.abort("digits input must only contain digits")
}
}

largestProduct {
var indices = 0..(_digits.count - _span)
var products = indices.map {|i| product_(_digits[i...(i + _span)])}
return max_(products)
}

product_(list) { list.reduce(1) {|prod, num| prod * num} }
max_(list) { list.reduce(0) {|max, prod| max.max(prod)} }
}
60 changes: 60 additions & 0 deletions exercises/practice/largest-series-product/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 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"

[88523f65-21ba-4458-a76a-b4aaf6e4cb5e]
description = "can find the largest product of 2 with numbers in order"

[f1376b48-1157-419d-92c2-1d7e36a70b8a]
description = "can find the largest product of 2"

[46356a67-7e02-489e-8fea-321c2fa7b4a4]
description = "can find the largest product of 3 with numbers in order"

[a2dcb54b-2b8f-4993-92dd-5ce56dece64a]
description = "can find the largest product of 3"

[673210a3-33cd-4708-940b-c482d7a88f9d]
description = "can find the largest product of 5 with numbers in order"

[02acd5a6-3bbf-46df-8282-8b313a80a7c9]
description = "can get the largest product of a big number"

[76dcc407-21e9-424c-a98e-609f269622b5]
description = "reports zero if the only digits are zero"

[6ef0df9f-52d4-4a5d-b210-f6fae5f20e19]
description = "reports zero if all spans include zero"

[5d81aaf7-4f67-4125-bf33-11493cc7eab7]
description = "rejects span longer than string length"

[06bc8b90-0c51-4c54-ac22-3ec3893a079e]
description = "reports 1 for empty string and empty product (0 span)"

[3ec0d92e-f2e2-4090-a380-70afee02f4c0]
description = "reports 1 for nonempty string and empty product (0 span)"

[6d96c691-4374-4404-80ee-2ea8f3613dd4]
description = "rejects empty string and nonzero span"

[7a38f2d6-3c35-45f6-8d6f-12e6e32d4d74]
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"
21 changes: 21 additions & 0 deletions exercises/practice/largest-series-product/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Exercism

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import "./largest-series-product" for Series
import "wren-testie/testie" for Testie, Expect

Testie.test("LargestSeriesProduct") { |do, skip|

do.test("finds the largest product if span equals length") {
var actual = Series.new("29", 2).largestProduct
var expected = 18
Expect.value(actual).toEqual(expected)
}

skip.test("can find the largest product of 2 with numbers in order") {
var actual = Series.new("0123456789", 2).largestProduct
var expected = 72
Expect.value(actual).toEqual(expected)
}

skip.test("can find the largest product of 2") {
var actual = Series.new("576802143", 2).largestProduct
var expected = 48
Expect.value(actual).toEqual(expected)
}

skip.test("can find the largest product of 3 with numbers in order") {
var actual = Series.new("0123456789", 3).largestProduct
var expected = 504
Expect.value(actual).toEqual(expected)
}

skip.test("can find the largest product of 3") {
var actual = Series.new("1027839564", 3).largestProduct
var expected = 270
Expect.value(actual).toEqual(expected)
}

skip.test("can find the largest product of 5 with numbers in order") {
var actual = Series.new("0123456789", 5).largestProduct
var expected = 15120
Expect.value(actual).toEqual(expected)
}

skip.test("can get the largest product of a big number") {
var actual = Series.new("73167176531330624919225119674426574742355349194934", 6).largestProduct
var expected = 23520
Expect.value(actual).toEqual(expected)
}

skip.test("reports zero if the only digits are zero") {
var actual = Series.new("0000", 2).largestProduct
var expected = 0
Expect.value(actual).toEqual(expected)
}

skip.test("reports zero if all spans include zero") {
var actual = Series.new("99099", 3).largestProduct
var expected = 0
Expect.value(actual).toEqual(expected)
}

skip.test("rejects span longer than string length") {
Expect.that {
Series.new("123", 4).largestProduct
}.abortsWith("span must be smaller than string length")
}

skip.test("reports 1 for empty string and empty product (0 span)") {
var actual = Series.new("", 0).largestProduct
var expected = 1
Expect.value(actual).toEqual(expected)
}

skip.test("reports 1 for nonempty string and empty product (0 span)") {
var actual = Series.new("123", 0).largestProduct
var expected = 1
Expect.value(actual).toEqual(expected)
}

skip.test("rejects empty string and nonzero span") {
Expect.that {
Series.new("", 1).largestProduct
}.abortsWith("span must be smaller than string length")
}

skip.test("rejects invalid character in digits") {
Expect.that {
Series.new("1234a5", 2).largestProduct
}.abortsWith("digits input must only contain digits")
}

skip.test("rejects negative span") {
Expect.that {
Series.new("12345", -1).largestProduct
}.abortsWith("span must not be negative")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Series {
construct new() {
Fiber.abort("Remove this statement and implement this function")
}
}
14 changes: 14 additions & 0 deletions exercises/practice/largest-series-product/package.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import "wren-package" for WrenPackage, Dependency
import "os" for Process

class Package is WrenPackage {
construct new() {}
name { "exercism/largest-series-product" }
dependencies {
return [
Dependency.new("wren-testie", "0.3.0", "https://github.com/joshgoebel/wren-testie.git")
]
}
}

Package.new().default()

0 comments on commit bb8417e

Please sign in to comment.