From f8351e3ab761d7dbe2d9691a85678b86469182de Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 19 Sep 2020 12:45:00 +0200 Subject: Solutions 078 Lubos Kolouch Perl Python --- challenge-078/lubos-kolouch/python/ch-1.py | 43 ++++++++++++++++++++++++++++++ challenge-078/lubos-kolouch/python/ch-2.py | 35 ++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 challenge-078/lubos-kolouch/python/ch-1.py create mode 100644 challenge-078/lubos-kolouch/python/ch-2.py (limited to 'challenge-078/lubos-kolouch/python') diff --git a/challenge-078/lubos-kolouch/python/ch-1.py b/challenge-078/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..ef18c534ac --- /dev/null +++ b/challenge-078/lubos-kolouch/python/ch-1.py @@ -0,0 +1,43 @@ +#!/bin/env python +""" https://perlweeklychallenge.org/blog/perl-weekly-challenge-078/ + Task 1 + Leader Element + 19.9.2020 Lubos Kolouch """ + + +class LeaderElements: + """ Class to find Leader elements from a list """ + + def __init__(self, arr: list()): + self.arr = arr + self.leaders = list() + + def process_list(self): + """ Process the list and fill in leaders """ + + if not self.arr: + return 0 + + self.leaders.insert(0, self.arr[-1]) + for item in reversed(self.arr): + if item > self.leaders[0]: + self.leaders.insert(0, item) + + return 1 + + def return_leaders(self): + """ Return the leaders list """ + return self.leaders + + +tester = LeaderElements([9, 10, 7, 5, 6, 1]) +tester.process_list() +assert tester.return_leaders() == [10, 7, 6, 1] + +tester = LeaderElements([3, 4, 5]) +tester.process_list() +assert tester.return_leaders() == [5] + +tester = LeaderElements([]) +tester.process_list() +assert tester.return_leaders() == [] diff --git a/challenge-078/lubos-kolouch/python/ch-2.py b/challenge-078/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..c1be12853b --- /dev/null +++ b/challenge-078/lubos-kolouch/python/ch-2.py @@ -0,0 +1,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]] -- cgit