aboutsummaryrefslogtreecommitdiff
path: root/challenge-078/lubos-kolouch/python/ch-2.py
blob: c1be12853b667c540136a89b81c22d7e62969245 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/env python
""" https://perlweeklychallenge.org/blog/perl-weekly-challenge-078/
    Task 2
    Left Rotation
    19.9.2020 Lubos Kolouch """


class ListRotator:
    """ Rotate the array based on list of elems """

    def __init__(self, arr: list(), elems: list()):
        self.arr = arr
        self.elems = elems

    def rotate_arr(self, first: int):
        """ Rotate the array so that passed elem is first """

        return self.arr[first:] + self.arr[0:first]

    def process_offset(self):
        """ Process the list of elems for rotation """

        output = list()

        for elem in self.elems:
            output.append(self.rotate_arr(elem))

        return output


test_list = ListRotator(arr=[10, 20, 30, 40, 50], elems=[3, 4])
assert test_list.process_offset() == [[40, 50, 10, 20, 30], [50, 10, 20, 30, 40]]

test_list = ListRotator(arr=[7, 4, 2, 6, 3], elems=[1, 3, 4])
assert test_list.process_offset() == [[4, 2, 6, 3, 7], [6, 3, 7, 4, 2], [3, 7, 4, 2, 6]]