-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "Largest series product" exercise (#47)
* add largest-series-product scaffold * sync docs * add first solution for largest-series-product * fix test names and add missing tests (make span signed int) * add euler big number test * refactor * perform digit validation during calculation * refactor + rename * turn loop for finding max into recursion * pass product struct to next_max_product * turn product_from into recursive func * Move new impl to example.cairo + revert lib.cairo to scaffold * Add missing practices + add error-handling concept * remove IndexOutOfBounds error * refactor+additional test
- Loading branch information
Nenad Misić
authored
Jun 27, 2024
1 parent
5603037
commit 6f5a0ed
Showing
17 changed files
with
487 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"blurb": "<todo>", | ||
"authors": [ | ||
"misicnenad" | ||
], | ||
"contributors": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Error Handling |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Introduction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
exercises/practice/largest-series-product/.docs/instructions.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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**. |
5 changes: 5 additions & 0 deletions
5
exercises/practice/largest-series-product/.docs/introduction.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
20 changes: 20 additions & 0 deletions
20
exercises/practice/largest-series-product/.meta/config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"authors": [ | ||
"misicnenad" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/lib.cairo", | ||
"Scarb.toml" | ||
], | ||
"test": [ | ||
"src/tests.cairo" | ||
], | ||
"example": [ | ||
".meta/example.cairo" | ||
] | ||
}, | ||
"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" | ||
} |
112 changes: 112 additions & 0 deletions
112
exercises/practice/largest-series-product/.meta/example.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#[derive(Drop, Debug, PartialEq)] | ||
pub enum Error { | ||
SpanTooLong, | ||
InvalidDigit: u8, | ||
NegativeSpan, | ||
IndexOutOfBounds | ||
} | ||
|
||
#[derive(Drop, Copy)] | ||
struct Product { | ||
value: u64, | ||
from: u32, | ||
} | ||
|
||
pub fn lsp(input: @ByteArray, span: i32) -> Result<u64, Error> { | ||
// validate span | ||
if span == 0 { | ||
return Result::Ok(1); | ||
} | ||
if span < 0 { | ||
return Result::Err(Error::NegativeSpan); | ||
} | ||
|
||
// shadowing to make span of unsigned type | ||
let span: u32 = span.try_into().unwrap(); | ||
|
||
if span > input.len() { | ||
return Result::Err(Error::SpanTooLong); | ||
} | ||
|
||
// calculate first max product | ||
// use '?' to propagate the error if it occurred | ||
let product = product_from(input, span, 0, span, Product { value: 1, from: 0 })?; | ||
|
||
next_max_product(input, span, product.value, product) | ||
} | ||
|
||
fn product_from( | ||
input: @ByteArray, span: u32, from: u32, remaining: u32, product: Product | ||
) -> Result<Product, Error> { | ||
if remaining == 0 { | ||
return Result::Ok(product); | ||
} | ||
if from + remaining > input.len() { | ||
return Result::Ok(Product { value: 0, from }); | ||
} | ||
|
||
let digit = input.at(from).try_into_digit()?; | ||
if digit != 0 { | ||
return product_from( | ||
input, | ||
span, | ||
from + 1, | ||
remaining - 1, | ||
Product { value: product.value * digit, from: product.from } | ||
); | ||
} else { | ||
return product_from(input, span, from + 1, span, Product { value: 1, from: from + 1 }); | ||
} | ||
} | ||
|
||
fn next_max_product( | ||
input: @ByteArray, span: u32, max: u64, current_product: Product | ||
) -> Result<u64, Error> { | ||
if current_product.from + span >= input.len() { | ||
return Result::Ok(max); | ||
} | ||
|
||
// safe to unwrap, we already processed this digit before | ||
let reduced_value = current_product.value | ||
/ input.at(current_product.from).try_into_digit().unwrap(); | ||
|
||
let next_digit = input.at(current_product.from + span).try_into_digit()?; | ||
|
||
let product = if next_digit == 0 { | ||
product_from( | ||
input, | ||
span, | ||
current_product.from + span + 1, | ||
span, | ||
Product { value: 1, from: current_product.from + span + 1 } | ||
)? | ||
} else { | ||
Product { value: reduced_value * next_digit, from: current_product.from + 1 } | ||
}; | ||
|
||
if product.value > max { | ||
next_max_product(input, span, product.value, product) | ||
} else { | ||
next_max_product(input, span, max, product) | ||
} | ||
} | ||
|
||
/// Helper functions | ||
#[generate_trait] | ||
impl ByteArrayCharToU64 of ByteArrayCharToU64Trait { | ||
fn try_into_digit(self: @Option<u8>) -> Result<u64, Error> { | ||
if let Option::Some(char) = (*self) { | ||
// ASCII digits are characters 48 through 57 | ||
if 48 <= char && char <= 57 { | ||
Result::Ok(char.into() - 48) | ||
} else { | ||
Result::Err(Error::InvalidDigit(char)) | ||
} | ||
} else { | ||
Result::Err(Error::IndexOutOfBounds) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests; |
60 changes: 60 additions & 0 deletions
60
exercises/practice/largest-series-product/.meta/tests.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[package] | ||
name = "largest_series_product" | ||
version = "0.1.0" | ||
edition = "2023_11" |
Oops, something went wrong.