From 022ecb91e5e277d377ba95c191ff8ed756aa7789 Mon Sep 17 00:00:00 2001 From: Eric Willigers Date: Fri, 3 May 2024 06:50:30 +1000 Subject: [PATCH] Add series exercise (#209) --- config.json | 8 +++ .../practice/series/.docs/instructions.md | 19 +++++ exercises/practice/series/.meta/config.json | 19 +++++ exercises/practice/series/.meta/example.v | 21 ++++++ exercises/practice/series/.meta/tests.toml | 36 ++++++++++ exercises/practice/series/run_test.v | 71 +++++++++++++++++++ exercises/practice/series/series.v | 4 ++ 7 files changed, 178 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.v create mode 100644 exercises/practice/series/.meta/tests.toml create mode 100644 exercises/practice/series/run_test.v create mode 100644 exercises/practice/series/series.v diff --git a/config.json b/config.json index 99ab652..c2311aa 100644 --- a/config.json +++ b/config.json @@ -631,6 +631,14 @@ "prerequisites": [], "difficulty": 3 }, + { + "slug": "series", + "name": "Series", + "uuid": "a0477a3c-547e-4cde-a23e-a917fa408284", + "practices": [], + "prerequisites": [], + "difficulty": 3 + }, { "slug": "etl", "name": "ETL", diff --git a/exercises/practice/series/.docs/instructions.md b/exercises/practice/series/.docs/instructions.md new file mode 100644 index 0000000..fd97a67 --- /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 0000000..345fc97 --- /dev/null +++ b/exercises/practice/series/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "keiravillekode" + ], + "files": { + "solution": [ + "series.v" + ], + "test": [ + "run_test.v" + ], + "example": [ + ".meta/example.v" + ] + }, + "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.v b/exercises/practice/series/.meta/example.v new file mode 100644 index 0000000..5564344 --- /dev/null +++ b/exercises/practice/series/.meta/example.v @@ -0,0 +1,21 @@ +module main + +fn slices(series string, slice_length int) ![]string { + if series.len == 0 { + return error('series cannot be empty') + } + if slice_length > series.len { + return error('slice length cannot be greater than series length') + } + if slice_length == 0 { + return error('slice length cannot be zero') + } + if slice_length < 0 { + return error('slice length cannot be negative') + } + mut result := []string{} + for i in 0 .. (series.len - slice_length + 1) { + result << series[i..(i + slice_length)] + } + return result +} diff --git a/exercises/practice/series/.meta/tests.toml b/exercises/practice/series/.meta/tests.toml new file mode 100644 index 0000000..9971434 --- /dev/null +++ b/exercises/practice/series/.meta/tests.toml @@ -0,0 +1,36 @@ +# This is an auto-generated file. Regular comments will be removed when this +# file is regenerated. Regenerating will not touch any manually added keys, +# so comments can be added in 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" + +[c7ed0812-0e4b-4bf3-99c4-28cbbfc246a2] +description = "empty series is invalid" diff --git a/exercises/practice/series/run_test.v b/exercises/practice/series/run_test.v new file mode 100644 index 0000000..b867bbe --- /dev/null +++ b/exercises/practice/series/run_test.v @@ -0,0 +1,71 @@ +module main + +fn test_slices_of_one_from_one() { + expected := ['1'] + assert slices('1', 1)! == expected +} + +fn test_slices_of_one_from_two() { + expected := ['1', '2'] + assert slices('12', 1)! == expected +} + +fn test_slices_of_two() { + expected := ['35'] + assert slices('35', 2)! == expected +} + +fn test_slices_of_two_overlap() { + expected := ['91', '14', '42'] + assert slices('9142', 2)! == expected +} + +fn test_slices_can_include_duplicates() { + expected := ['777', '777', '777', '777'] + assert slices('777777', 3)! == expected +} + +fn test_slices_of_a_long_series() { + expected := ['91849', '18493', '84939', '49390', '93904', '39042', '90424', '04243'] + assert slices('918493904243', 5)! == expected +} + +fn test_slice_length_is_too_large() { + if res := slices('12345', 6) { + assert false, 'slice length too large should return an error' + } else { + assert err.msg() == 'slice length cannot be greater than series length' + } +} + +fn test_slice_length_is_way_too_large() { + if res := slices('12345', 42) { + assert false, 'slice length way too large should return an error' + } else { + assert err.msg() == 'slice length cannot be greater than series length' + } +} + +fn test_slice_length_cannot_be_zero() { + if res := slices('12345', 0) { + assert false, 'slice length cannot be zero should return an error' + } else { + assert err.msg() == 'slice length cannot be zero' + } +} + +fn test_slice_length_cannot_be_negative() { + if res := slices('123', -1) { + assert false, 'slice length cannot be negative should return an error' + } else { + assert err.msg() == 'slice length cannot be negative' + } +} + +fn test_empty_series_is_invalid() { + if res := slices('', 1) { + assert false, 'empty series invalid should return an error' + } else { + assert err.msg() == 'series cannot be empty' + } +} diff --git a/exercises/practice/series/series.v b/exercises/practice/series/series.v new file mode 100644 index 0000000..0ee1949 --- /dev/null +++ b/exercises/practice/series/series.v @@ -0,0 +1,4 @@ +module main + +fn slices(series string, slice_length int) ![]string { +}