aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2020-03-25 22:35:35 +0100
committerLubos Kolouch <lubos@kolouch.net>2020-03-25 22:35:35 +0100
commit09f158a7187f117b5776bb8dc2bb22e6fa22f042 (patch)
tree5eaab6747a361d5b36b99497fcba1b3bcdd48c9f
parentf080ec06068137803b07f642bce5f6c2126ca1b8 (diff)
downloadperlweeklychallenge-club-09f158a7187f117b5776bb8dc2bb22e6fa22f042.tar.gz
perlweeklychallenge-club-09f158a7187f117b5776bb8dc2bb22e6fa22f042.tar.bz2
perlweeklychallenge-club-09f158a7187f117b5776bb8dc2bb22e6fa22f042.zip
Task 1 Python
-rw-r--r--challenge-053/lubos-kolouch/python/ch-1.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/challenge-053/lubos-kolouch/python/ch-1.py b/challenge-053/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..843c17a699
--- /dev/null
+++ b/challenge-053/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,60 @@
+#!/usr/bin/env python
+
+import numpy as np
+import pytest
+
+
+class ArrRotator:
+
+ def __init__(self):
+ self.arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
+
+ def check_degree(self, degree: int):
+ if degree not in (90, 180, 270):
+ raise ValueError('degree must be 90, 180 or 270')
+
+ def rotate_arr(self, degree: int):
+ self.check_degree(degree)
+
+ result_arr = self.arr
+
+ for i in range(degree // 90):
+ result_arr = np.rot90(result_arr, -1)
+
+ return result_arr
+
+
+class RotatorTest:
+ # TESTS
+
+ def __init__(self):
+ self.rotator = ArrRotator()
+
+ def do_tests(self):
+
+ assert np.alltrue(self.rotator.rotate_arr(degree=90)
+ == [[7, 4, 1], [8, 5, 2], [9, 6, 3]])
+ assert np.alltrue(self.rotator.rotate_arr(degree=180)
+ == [[9, 8, 7], [6, 5, 4], [3, 2, 1]])
+ assert np.alltrue(self.rotator.rotate_arr(degree=270)
+ == [[3, 6, 9], [2, 5, 8], [1, 4, 7]])
+ with pytest.raises(ValueError):
+ rotator.rotate_arr(degree=222)
+
+
+if __name__ == "__main__":
+ rotator = ArrRotator()
+
+ print('Original array:')
+ print(rotator.arr)
+ print('Rotate by 90 : ')
+ print(rotator.rotate_arr(degree=90))
+
+ print('Rotate by 180 : ')
+ print(rotator.rotate_arr(degree=180))
+
+ print('Rotate by 270 : ')
+ print(rotator.rotate_arr(degree=270))
+
+ tester = RotatorTest()
+ tester.do_tests()