Skip to content

Commit fde2e5b

Browse files
sync series (#318)
1 parent 7904493 commit fde2e5b

3 files changed

Lines changed: 44 additions & 24 deletions

File tree

exercises/practice/series/.meta/tests.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ description = "slices of a long series"
3030
[6d235d85-46cf-4fae-9955-14b6efef27cd]
3131
description = "slice length is too large"
3232

33+
[d7957455-346d-4e47-8e4b-87ed1564c6d7]
34+
description = "slice length is way too large"
35+
3336
[d34004ad-8765-4c09-8ba1-ada8ce776806]
3437
description = "slice length cannot be zero"
3538

exercises/practice/series/example/series.d

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ int[] digits(string input)
6161

6262
int[][] slice(string input, uint stride = 1)
6363
{
64+
if (stride == 0)
65+
throw new Exception("slice length cannot be zero");
66+
6467
if (input.length < stride)
6568
throw new Exception("Stride is longer than input.");
6669

exercises/practice/series/source/series.d

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,63 +50,77 @@ unittest
5050
assert(equal(actual, expected));
5151
}
5252

53-
// can_slice_by_2
53+
// Slices of one from one
5454
{
55-
const int[][] expected = [
56-
[9, 8], [8, 2], [2, 7], [7, 3], [3, 4], [4, 6], [6, 3]
57-
];
58-
const int[][] actual = slice("98273463", 2);
55+
const int[][] expected = [[1]];
56+
const int[][] actual = slice("1", 1);
5957

6058
assert(equal(actual, expected));
6159
}
6260

63-
// can_slice_by_3
61+
// Slices of one from two
6462
{
65-
const int[][] expected = [[0, 1, 2], [1, 2, 3], [2, 3, 4]];
66-
const int[][] actual = slice("01234", 3);
63+
const int[][] expected = [[1], [2]];
64+
const int[][] actual = slice("12", 1);
6765

6866
assert(equal(actual, expected));
6967
}
7068

71-
// can_slice_by_3_with_duplicate_digits
69+
// Slices of two
7270
{
73-
const int[][] expected = [[3, 1, 0], [1, 0, 0], [0, 0, 1]];
74-
const int[][] actual = slice("31001", 3);
71+
const int[][] expected = [[3, 5]];
72+
const int[][] actual = slice("35", 2);
7573

7674
assert(equal(actual, expected));
7775
}
7876

79-
// can_slice_by_4
77+
// Slices of two overlap
8078
{
81-
const int[][] expected = [[3, 1, 0], [1, 0, 0], [0, 0, 1]];
82-
const int[][] actual = slice("31001", 3);
79+
const int[][] expected = [[9, 1], [1, 4], [4, 2]];
80+
const int[][] actual = slice("9142", 2);
8381

8482
assert(equal(actual, expected));
8583
}
8684

87-
// can_slice_by_5
85+
// Slices can include duplicates
8886
{
89-
const int[][] expected = [[8, 1, 2, 2, 8]];
90-
const int[][] actual = slice("81228", 5);
87+
const int[][] expected = [[7, 7, 7], [7, 7, 7], [7, 7, 7], [7, 7, 7]];
88+
const int[][] actual = slice("777777", 3);
9189

9290
assert(equal(actual, expected));
9391
}
9492

95-
// exception_if_not_enough_digits_to_slice
93+
// Slices of a long series
9694
{
97-
assertThrown(slice("01032987583", 12));
95+
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]];
96+
const int[][] actual = slice("918493904243", 5);
97+
98+
assert(equal(actual, expected));
9899
}
99100

100-
// exception_if_invalid_input
101+
// Slice length is too large
101102
{
102-
assertThrown(slice("01032i987583", 12));
103+
assertThrown(slice("12345", 6));
103104
}
104105

105-
// exception_if_invalid_input_2
106+
// Slice length is way too large
106107
{
107-
assertThrown(digits("01032i987583"));
108+
assertThrown(slice("12345", 42));
108109
}
109110

110-
}
111+
// Slice length cannot be zero
112+
{
113+
assertThrown(slice("12345", 0));
114+
}
111115

116+
// Slice length cannot be negative
117+
{
118+
assertThrown(slice("123", -1));
119+
}
120+
121+
// Empty series is invalid
122+
{
123+
assertThrown(slice("", 1));
124+
}
125+
}
112126
}

0 commit comments

Comments
 (0)