From d80b80e1cf4577fc6bc6bc8a57d8cda7812a3c8b Mon Sep 17 00:00:00 2001 From: Nenad Date: Tue, 1 Oct 2024 09:51:40 +0200 Subject: [PATCH] Implement series exercise --- config.json | 8 ++ .../practice/series/.docs/instructions.md | 19 ++++ exercises/practice/series/.meta/config.json | 22 +++++ exercises/practice/series/.meta/example.cairo | 19 ++++ exercises/practice/series/.meta/tests.toml | 45 +++++++++ exercises/practice/series/Scarb.toml | 7 ++ exercises/practice/series/src/lib.cairo | 3 + exercises/practice/series/tests/series.cairo | 96 +++++++++++++++++++ 8 files changed, 219 insertions(+) create mode 100644 exercises/practice/series/.docs/instructions.md create mode 100644 exercises/practice/series/.meta/config.json create mode 100644 exercises/practice/series/.meta/example.cairo create mode 100644 exercises/practice/series/.meta/tests.toml create mode 100644 exercises/practice/series/Scarb.toml create mode 100644 exercises/practice/series/src/lib.cairo create mode 100644 exercises/practice/series/tests/series.cairo diff --git a/config.json b/config.json index dffba7f3..2de4b60b 100644 --- a/config.json +++ b/config.json @@ -755,6 +755,14 @@ "practices": [], "prerequisites": [], "difficulty": 1 + }, + { + "slug": "series", + "name": "Series", + "uuid": "b7a0428f-af52-49a5-a768-65197b9dfaa9", + "practices": [], + "prerequisites": [], + "difficulty": 2 } ], "foregone": [ diff --git a/exercises/practice/series/.docs/instructions.md b/exercises/practice/series/.docs/instructions.md new file mode 100644 index 00000000..fd97a670 --- /dev/null +++ b/exercises/practice/series/.docs/instructions.md @@ -0,0 +1,19 @@ +# Instructions + +Given a string of digits, output all the contiguous substrings of length `n` in that string in the order that they appear. + +For example, the string "49142" has the following 3-digit series: + +- "491" +- "914" +- "142" + +And the following 4-digit series: + +- "4914" +- "9142" + +And if you ask for a 6-digit series from a 5-digit string, you deserve whatever you get. + +Note that these series are only required to occupy _adjacent positions_ in the input; +the digits need not be _numerically consecutive_. diff --git a/exercises/practice/series/.meta/config.json b/exercises/practice/series/.meta/config.json new file mode 100644 index 00000000..f02ee823 --- /dev/null +++ b/exercises/practice/series/.meta/config.json @@ -0,0 +1,22 @@ +{ + "authors": [ + "0xNeshi" + ], + "files": { + "solution": [ + "src/lib.cairo" + ], + "test": [ + "tests/series.cairo" + ], + "example": [ + ".meta/example.cairo" + ], + "invalidator": [ + "Scarb.toml" + ] + }, + "blurb": "Given a string of digits, output all the contiguous substrings of length `n` in that string.", + "source": "A subset of the Problem 8 at Project Euler", + "source_url": "https://projecteuler.net/problem=8" +} diff --git a/exercises/practice/series/.meta/example.cairo b/exercises/practice/series/.meta/example.cairo new file mode 100644 index 00000000..602d8d29 --- /dev/null +++ b/exercises/practice/series/.meta/example.cairo @@ -0,0 +1,19 @@ +pub fn slices(series: ByteArray, slice_length: usize) -> Array { + assert!(series.len() != 0, "series cannot be empty"); + assert!(slice_length != 0, "slice length cannot be zero"); + assert!(slice_length <= series.len(), "slice length cannot be greater than series length"); + + let max_start = series.len() - slice_length + 1; + + let mut all_slices = array![]; + for i in 0 + ..max_start { + let mut slice = ""; + for j in i..(i + slice_length) { + slice.append_byte(series[j]); + }; + all_slices.append(slice); + }; + + all_slices +} diff --git a/exercises/practice/series/.meta/tests.toml b/exercises/practice/series/.meta/tests.toml new file mode 100644 index 00000000..79345a10 --- /dev/null +++ b/exercises/practice/series/.meta/tests.toml @@ -0,0 +1,45 @@ +# 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. + +[7ae7a46a-d992-4c2a-9c15-a112d125ebad] +description = "slices of one from one" + +[3143b71d-f6a5-4221-aeae-619f906244d2] +description = "slices of one from two" + +[dbb68ff5-76c5-4ccd-895a-93dbec6d5805] +description = "slices of two" + +[19bbea47-c987-4e11-a7d1-e103442adf86] +description = "slices of two overlap" + +[8e17148d-ba0a-4007-a07f-d7f87015d84c] +description = "slices can include duplicates" + +[bd5b085e-f612-4f81-97a8-6314258278b0] +description = "slices of a long series" + +[6d235d85-46cf-4fae-9955-14b6efef27cd] +description = "slice length is too large" + +[d7957455-346d-4e47-8e4b-87ed1564c6d7] +description = "slice length is way too large" + +[d34004ad-8765-4c09-8ba1-ada8ce776806] +description = "slice length cannot be zero" + +[10ab822d-8410-470a-a85d-23fbeb549e54] +description = "slice length cannot be negative" +include = false +comment = "Cairo can exclude negative integers with its type system" + +[c7ed0812-0e4b-4bf3-99c4-28cbbfc246a2] +description = "empty series is invalid" diff --git a/exercises/practice/series/Scarb.toml b/exercises/practice/series/Scarb.toml new file mode 100644 index 00000000..024a38a1 --- /dev/null +++ b/exercises/practice/series/Scarb.toml @@ -0,0 +1,7 @@ +[package] +name = "series" +version = "0.1.0" +edition = "2024_07" + +[dev-dependencies] +cairo_test = "2.8.2" diff --git a/exercises/practice/series/src/lib.cairo b/exercises/practice/series/src/lib.cairo new file mode 100644 index 00000000..c8578313 --- /dev/null +++ b/exercises/practice/series/src/lib.cairo @@ -0,0 +1,3 @@ +pub fn slices(series: ByteArray, slice_length: usize) -> Array { + panic!("implement `slices`") +} diff --git a/exercises/practice/series/tests/series.cairo b/exercises/practice/series/tests/series.cairo new file mode 100644 index 00000000..23273596 --- /dev/null +++ b/exercises/practice/series/tests/series.cairo @@ -0,0 +1,96 @@ +use series::slices; + +#[test] +fn slices_of_one_from_one() { + let input = "1"; + let length = 1; + let output = slices(input, length); + let expected = array!["1"]; + assert_eq!(output, expected); +} + +#[test] +#[ignore] +fn slices_of_one_from_two() { + let input = "12"; + let length = 1; + let output = slices(input, length); + let expected = array!["1", "2"]; + assert_eq!(output, expected); +} + +#[test] +#[ignore] +fn slices_of_two() { + let input = "35"; + let length = 2; + let output = slices(input, length); + let expected = array!["35"]; + assert_eq!(output, expected); +} + +#[test] +#[ignore] +fn slices_of_two_overlap() { + let input = "9142"; + let length = 2; + let output = slices(input, length); + let expected = array!["91", "14", "42"]; + assert_eq!(output, expected); +} + +#[test] +#[ignore] +fn slices_can_include_duplicates() { + let input = "777777"; + let length = 3; + let output = slices(input, length); + let expected = array!["777", "777", "777", "777"]; + assert_eq!(output, expected); +} + +#[test] +#[ignore] +fn slices_of_a_long_series() { + let input = "918493904243"; + let length = 5; + let output = slices(input, length); + let expected = array!["91849", "18493", "84939", "49390", "93904", "39042", "90424", "04243",]; + assert_eq!(output, expected); +} + +#[test] +#[ignore] +#[should_panic(expected: ("slice length cannot be greater than series length",))] +fn slice_length_is_too_large() { + let input = "12345"; + let length = 6; + slices(input, length); +} + +#[test] +#[ignore] +#[should_panic(expected: ("slice length cannot be greater than series length",))] +fn slice_length_is_way_too_large() { + let input = "12345"; + let length = 42; + slices(input, length); +} + +#[test] +#[ignore] +#[should_panic(expected: ("slice length cannot be zero",))] +fn slice_length_cannot_be_zero() { + let input = "12345"; + let length = 0; + slices(input, length); +} + +#[test] +#[ignore] +#[should_panic(expected: ("series cannot be empty",))] +fn empty_series_is_invalid() { + let input = ""; + let length = 1; + slices(input, length); +}