From 9f29500ccbf691463174bc346ed7a2e367c4717d Mon Sep 17 00:00:00 2001 From: Eric Willigers Date: Fri, 4 Jul 2025 11:53:58 +1000 Subject: [PATCH] sync series --- exercises/practice/series/.meta/tests.toml | 3 ++ exercises/practice/series/example/series.d | 3 ++ exercises/practice/series/source/series.d | 62 +++++++++++++--------- 3 files changed, 44 insertions(+), 24 deletions(-) diff --git a/exercises/practice/series/.meta/tests.toml b/exercises/practice/series/.meta/tests.toml index 35e58921..9696f51f 100644 --- a/exercises/practice/series/.meta/tests.toml +++ b/exercises/practice/series/.meta/tests.toml @@ -30,6 +30,9 @@ 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" diff --git a/exercises/practice/series/example/series.d b/exercises/practice/series/example/series.d index c49c8d01..ab50d9bd 100644 --- a/exercises/practice/series/example/series.d +++ b/exercises/practice/series/example/series.d @@ -61,6 +61,9 @@ int[] digits(string input) int[][] slice(string input, uint stride = 1) { + if (stride == 0) + throw new Exception("slice length cannot be zero"); + if (input.length < stride) throw new Exception("Stride is longer than input."); diff --git a/exercises/practice/series/source/series.d b/exercises/practice/series/source/series.d index c9a864c8..7245e56b 100644 --- a/exercises/practice/series/source/series.d +++ b/exercises/practice/series/source/series.d @@ -50,63 +50,77 @@ unittest assert(equal(actual, expected)); } - // can_slice_by_2 + // Slices of one from one { - const int[][] expected = [ - [9, 8], [8, 2], [2, 7], [7, 3], [3, 4], [4, 6], [6, 3] - ]; - const int[][] actual = slice("98273463", 2); + const int[][] expected = [[1]]; + const int[][] actual = slice("1", 1); assert(equal(actual, expected)); } - // can_slice_by_3 + // Slices of one from two { - const int[][] expected = [[0, 1, 2], [1, 2, 3], [2, 3, 4]]; - const int[][] actual = slice("01234", 3); + const int[][] expected = [[1], [2]]; + const int[][] actual = slice("12", 1); assert(equal(actual, expected)); } - // can_slice_by_3_with_duplicate_digits + // Slices of two { - const int[][] expected = [[3, 1, 0], [1, 0, 0], [0, 0, 1]]; - const int[][] actual = slice("31001", 3); + const int[][] expected = [[3, 5]]; + const int[][] actual = slice("35", 2); assert(equal(actual, expected)); } - // can_slice_by_4 + // Slices of two overlap { - const int[][] expected = [[3, 1, 0], [1, 0, 0], [0, 0, 1]]; - const int[][] actual = slice("31001", 3); + const int[][] expected = [[9, 1], [1, 4], [4, 2]]; + const int[][] actual = slice("9142", 2); assert(equal(actual, expected)); } - // can_slice_by_5 + // Slices can include duplicates { - const int[][] expected = [[8, 1, 2, 2, 8]]; - const int[][] actual = slice("81228", 5); + const int[][] expected = [[7, 7, 7], [7, 7, 7], [7, 7, 7], [7, 7, 7]]; + const int[][] actual = slice("777777", 3); assert(equal(actual, expected)); } - // exception_if_not_enough_digits_to_slice + // Slices of a long series { - assertThrown(slice("01032987583", 12)); + const int[][] expected = [[9, 1, 8, 4, 9], [1, 8, 4, 9, 3], [8, 4, 9, 3, 9], [4, 9, 3, 9, 0], [9, 3, 9, 0, 4], [3, 9, 0, 4, 2], [9, 0, 4, 2, 4], [0, 4, 2, 4, 3]]; + const int[][] actual = slice("918493904243", 5); + + assert(equal(actual, expected)); } - // exception_if_invalid_input + // Slice length is too large { - assertThrown(slice("01032i987583", 12)); + assertThrown(slice("12345", 6)); } - // exception_if_invalid_input_2 + // Slice length is way too large { - assertThrown(digits("01032i987583")); + assertThrown(slice("12345", 42)); } - } + // Slice length cannot be zero + { + assertThrown(slice("12345", 0)); + } + // Slice length cannot be negative + { + assertThrown(slice("123", -1)); + } + + // Empty series is invalid + { + assertThrown(slice("", 1)); + } + } }