Skip to content

Commit 178d0b9

Browse files
mrcfpscmccandless
authored andcommitted
series: re-implement according to canonical data (exercism#1379)
* series: improve documentation * series: re-implement according to cannonical data
1 parent fc024e1 commit 178d0b9

3 files changed

Lines changed: 35 additions & 37 deletions

File tree

exercises/series/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# Series
22

33
Given a string of digits, output all the contiguous substrings of length `n` in
4-
that string.
4+
that string in the order that they appear.
55

66
For example, the string "49142" has the following 3-digit series:
77

8-
- 491
9-
- 914
10-
- 142
8+
- "491"
9+
- "914"
10+
- "142"
1111

1212
And the following 4-digit series:
1313

14-
- 4914
15-
- 9142
14+
- "4914"
15+
- "9142"
1616

1717
And if you ask for a 6-digit series from a 5-digit string, you deserve
1818
whatever you get.

exercises/series/example.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
def slices(series, length):
2-
numbers = [int(digit) for digit in series]
3-
if not 1 <= length <= len(numbers):
2+
if not 1 <= length <= len(series):
43
raise ValueError("Invalid slice length for this series: " + str(
54
length))
6-
return [numbers[i:i + length] for i in range(len(numbers) - length + 1)]
5+
return [series[i:i + length] for i in range(len(series) - length + 1)]

exercises/series/series_test.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,39 @@
99
from series import slices
1010

1111

12+
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0
13+
1214
class SeriesTest(unittest.TestCase):
13-
def test_slices_of_one(self):
14-
self.assertEqual(
15-
slices("01234", 1),
16-
[[0], [1], [2], [3], [4]], )
15+
def test_slices_of_one_from_one(self):
16+
self.assertEqual(slices("1", 1), ["1"])
17+
18+
def test_slices_of_one_from_two(self):
19+
self.assertEqual(slices("12", 1), ["1", "2"])
1720

1821
def test_slices_of_two(self):
19-
self.assertEqual(
20-
slices("97867564", 2),
21-
[[9, 7], [7, 8], [8, 6], [6, 7], [7, 5], [5, 6], [6, 4]], )
22-
23-
def test_slices_of_three(self):
24-
self.assertEqual(
25-
slices("97867564", 3),
26-
[[9, 7, 8], [7, 8, 6], [8, 6, 7], [6, 7, 5], [7, 5, 6], [5, 6, 4]],
27-
)
28-
29-
def test_slices_of_four(self):
30-
self.assertEqual(
31-
slices("01234", 4),
32-
[[0, 1, 2, 3], [1, 2, 3, 4]], )
33-
34-
def test_slices_of_five(self):
35-
self.assertEqual(
36-
slices("01234", 5),
37-
[[0, 1, 2, 3, 4]], )
38-
39-
def test_overly_long_slice(self):
22+
self.assertEqual(slices("35", 2), ["35"])
23+
24+
def test_slices_of_two_overlap(self):
25+
self.assertEqual(slices("9142", 2), ["91", "14", "42"])
26+
27+
def test_slices_can_include_duplicates(self):
28+
self.assertEqual(slices("777777", 3), ["777", "777", "777", "777"])
29+
30+
def test_slice_length_is_too_large(self):
31+
with self.assertRaisesWithMessage(ValueError):
32+
slices("12345", 6)
33+
34+
def test_slice_length_cannot_be_zero(self):
35+
with self.assertRaisesWithMessage(ValueError):
36+
slices("12345", 0)
37+
38+
def test_slice_length_cannot_be_negative(self):
4039
with self.assertRaisesWithMessage(ValueError):
41-
slices("012", 4)
40+
slices("123", -1)
4241

43-
def test_overly_short_slice(self):
42+
def test_empty_series_is_invalid(self):
4443
with self.assertRaisesWithMessage(ValueError):
45-
slices("01234", 0)
44+
slices("", 1)
4645

4746
# Utility functions
4847
def setUp(self):

0 commit comments

Comments
 (0)