diff options
Diffstat (limited to 'challenge-272/roger-bell-west/python/ch-2.py')
| -rwxr-xr-x | challenge-272/roger-bell-west/python/ch-2.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/challenge-272/roger-bell-west/python/ch-2.py b/challenge-272/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..4332977373 --- /dev/null +++ b/challenge-272/roger-bell-west/python/ch-2.py @@ -0,0 +1,36 @@ +#! /usr/bin/python3 + +import collections +from itertools import islice + +# https://docs.python.org/3/library/itertools.html +def sliding_window(iterable, n): + # sliding_window('ABCDEFG', 4) --> ABCD BCDE CDEF DEFG + it = iter(iterable) + window = collections.deque(islice(it, n), maxlen=n) + if len(window) == n: + yield tuple(window) + for x in it: + window.append(x) + yield tuple(window) + +def stringscore(a): + out = 0 + for i in sliding_window(a, 2): + out += abs(ord(i[0]) - ord(i[1])) + return out + +import unittest + +class TestStringscore(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(stringscore("hello"), 13, 'example 1') + + def test_ex2(self): + self.assertEqual(stringscore("perl"), 30, 'example 2') + + def test_ex3(self): + self.assertEqual(stringscore("raku"), 37, 'example 3') + +unittest.main() |
