diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-08-10 20:00:56 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-08-10 20:00:56 +0200 |
| commit | 407bdf405e1cc65708dc231c0a236d79c327e8d3 (patch) | |
| tree | e178f86a700ad09d55b0b71d64282066e6e1dd08 /challenge-200/lubos-kolouch/python/ch-1.py | |
| parent | 2cbddb9ecfe878d6e3b10d472e6647e6229ad1ce (diff) | |
| download | perlweeklychallenge-club-407bdf405e1cc65708dc231c0a236d79c327e8d3.tar.gz perlweeklychallenge-club-407bdf405e1cc65708dc231c0a236d79c327e8d3.tar.bz2 perlweeklychallenge-club-407bdf405e1cc65708dc231c0a236d79c327e8d3.zip | |
feat(challenge-200/lubos-kolouch/): Challenge 200 LK Perl Python Blog
Diffstat (limited to 'challenge-200/lubos-kolouch/python/ch-1.py')
| -rw-r--r-- | challenge-200/lubos-kolouch/python/ch-1.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/challenge-200/lubos-kolouch/python/ch-1.py b/challenge-200/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..a451ddaeee --- /dev/null +++ b/challenge-200/lubos-kolouch/python/ch-1.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import unittest + + +def arithmetic_slices(lst): + if len(lst) < 3: + return [] + + slices = [] + for i in range(len(lst) - 2): + if lst[i + 1] - lst[i] == lst[i + 2] - lst[i + 1]: + diff = lst[i + 1] - lst[i] + slice = [lst[i], lst[i + 1], lst[i + 2]] + slices.append(slice[:]) + for j in range(i + 3, len(lst)): + if lst[j] - lst[j - 1] == diff: + slice.append(lst[j]) + slices.append(slice[:]) + else: + break + + return slices + + +class TestArithmeticSlices(unittest.TestCase): + + def test_cases(self): + self.assertEqual(arithmetic_slices([1, 2, 3, 4]), + [[1, 2, 3], [1, 2, 3, 4], [2, 3, 4]], 'Test Case 1') + self.assertEqual(arithmetic_slices([2]), [], 'Test Case 2') + + +if __name__ == '__main__': + unittest.main() |
