forked from exercism/ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request exercism#10 from sjakobi/series
New exercise: python series
- Loading branch information
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ word-count | |
anagram | ||
beer-song | ||
nucleotide-count | ||
series | ||
point-mutations | ||
phone-number | ||
grade-school | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Series(object): | ||
def __init__(self, digits): | ||
self.digits = digits | ||
self.numbers = [int(d) for d in digits] | ||
|
||
def slices(self, length): | ||
if not 1 <= length <= len(self.numbers): | ||
raise ValueError("Invalid slice length for this series: " + | ||
str(length)) | ||
return [self.numbers[i:i + length] | ||
for i in range(len(self.numbers) - length + 1)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
try: | ||
from series import Series | ||
except ImportError: | ||
raise SystemExit('Could not find series.py. Does it exist?') | ||
|
||
import unittest | ||
|
||
|
||
class SeriesTest(unittest.TestCase): | ||
def test_slices_of_one(self): | ||
self.assertEqual([[0], [1], [2], [3], [4]], | ||
Series("01234").slices(1)) | ||
|
||
def test_slices_of_two(self): | ||
self.assertEqual([[9, 7], [7, 8], [8, 6], [6, 7], | ||
[7, 5], [5, 6], [6, 4]], | ||
Series("97867564").slices(2)) | ||
|
||
def test_slices_of_three(self): | ||
self.assertEqual([[9, 7, 8], [7, 8, 6], [8, 6, 7], | ||
[6, 7, 5], [7, 5, 6], [5, 6, 4]], | ||
Series("97867564").slices(3)) | ||
|
||
def test_slices_of_four(self): | ||
self.assertEqual([[0, 1, 2, 3], [1, 2, 3, 4]], | ||
Series("01234").slices(4)) | ||
|
||
def test_slices_of_five(self): | ||
self.assertEqual([[0, 1, 2, 3, 4]], | ||
Series("01234").slices(5)) | ||
|
||
def test_overly_long_slice(self): | ||
self.assertRaisesRegexp(ValueError, | ||
"^Invalid slice length for this series: 4$", | ||
Series("012").slices, 4) | ||
|
||
def test_overly_short_slice(self): | ||
self.assertRaisesRegexp(ValueError, | ||
"^Invalid slice length for this series: 0$", | ||
Series("01234").slices, 0) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |