diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2024-09-16 12:18:36 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2024-09-16 12:18:36 +0200 |
| commit | d8b05f99de150566a266dec2b0a7590f373bb5dc (patch) | |
| tree | 1d5d0b221eebd9431fd452a3c6fe5f1327bb5058 /challenge-286/lubos-kolouch/python | |
| parent | 2efac178f2b76e3afa2f45e91e288469805ab1b1 (diff) | |
| download | perlweeklychallenge-club-d8b05f99de150566a266dec2b0a7590f373bb5dc.tar.gz perlweeklychallenge-club-d8b05f99de150566a266dec2b0a7590f373bb5dc.tar.bz2 perlweeklychallenge-club-d8b05f99de150566a266dec2b0a7590f373bb5dc.zip | |
Challenge 286 LK Perl Python
Diffstat (limited to 'challenge-286/lubos-kolouch/python')
| -rw-r--r-- | challenge-286/lubos-kolouch/python/ch-1.py | 40 | ||||
| -rw-r--r-- | challenge-286/lubos-kolouch/python/ch-2.py | 64 |
2 files changed, 104 insertions, 0 deletions
diff --git a/challenge-286/lubos-kolouch/python/ch-1.py b/challenge-286/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..b47c5340ff --- /dev/null +++ b/challenge-286/lubos-kolouch/python/ch-1.py @@ -0,0 +1,40 @@ +import os +import random +import unittest +from typing import List + + +def get_random_word() -> str: + """ + Reads its own source code, splits it into words, and returns one word at random. + + Returns: + str: A random word from the script's source code. + """ + filename: str = os.path.abspath(__file__) + with open(filename, 'r') as f: + content: str = f.read() + words: List[str] = content.split() + random_word: str = random.choice(words) + return random_word + + +def main() -> None: + word: str = get_random_word() + print(word) + + +if __name__ == "__main__": + main() + + # Unit Test + class TestSelfSpammer(unittest.TestCase): + + def test_word_in_source(self): + word = get_random_word() + filename = os.path.abspath(__file__) + with open(filename, 'r') as f: + content = f.read() + self.assertIn(word, content.split(), "Word is from source code") + + unittest.main(argv=['first-arg-is-ignored'], exit=False) diff --git a/challenge-286/lubos-kolouch/python/ch-2.py b/challenge-286/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..212c9ac1da --- /dev/null +++ b/challenge-286/lubos-kolouch/python/ch-2.py @@ -0,0 +1,64 @@ +from typing import List +import unittest + + +def order_game(ints: List[int]) -> int: + """ + Simulates the Order Game on the given list of integers. + + In the Order Game: + - At each round, the array is reduced by pairing elements. + - For each pair at an even index (starting from 0), take the minimum of the pair. + - For each pair at an odd index, take the maximum of the pair. + - Repeat the process until only one element remains. + + Args: + ints (List[int]): A list of integers whose length is a power of 2. + + Returns: + int: The last remaining integer after playing the game. + """ + current_list = ints.copy() + while len(current_list) > 1: + next_list = [] + n = len(current_list) + for i in range(n // 2): + if i % 2 == 0: + # Even index: take minimum + next_list.append( + min(current_list[2 * i], current_list[2 * i + 1])) + else: + # Odd index: take maximum + next_list.append( + max(current_list[2 * i], current_list[2 * i + 1])) + current_list = next_list + return current_list[0] + + +# Unit Tests +class TestOrderGame(unittest.TestCase): + + def test_example1(self): + ints = [2, 1, 4, 5, 6, 3, 0, 2] + self.assertEqual(order_game(ints), 1, 'Example 1') + + def test_example2(self): + ints = [0, 5, 3, 2] + self.assertEqual(order_game(ints), 0, 'Example 2') + + def test_example3(self): + ints = [9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8] + self.assertEqual(order_game(ints), 2, 'Example 3') + + def test_power_of_two_length(self): + ints = [1, 2] + self.assertEqual(order_game(ints), 1, 'Length 2 array') + + def test_large_array(self): + ints = [i for i in range(16)] + result = order_game(ints) + self.assertIsInstance(result, int, 'Result is an integer') + + +if __name__ == "__main__": + unittest.main() |
