Skip to content

Commit

Permalink
Merge pull request exercism#10 from sjakobi/series
Browse files Browse the repository at this point in the history
New exercise: python series
  • Loading branch information
kytrinyx committed Mar 18, 2014
2 parents 736ec59 + b702c21 commit 4450501
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions EXERCISES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ word-count
anagram
beer-song
nucleotide-count
series
point-mutations
phone-number
grade-school
Expand Down
11 changes: 11 additions & 0 deletions series/example.py
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)]
44 changes: 44 additions & 0 deletions series/series_test.py
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()

0 comments on commit 4450501

Please sign in to comment.