Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions exercises/practice/series/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
3 changes: 3 additions & 0 deletions exercises/practice/series/example/series.d
Original file line number Diff line number Diff line change
Expand Up @@ -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.");

Expand Down
62 changes: 38 additions & 24 deletions exercises/practice/series/source/series.d
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}