aboutsummaryrefslogtreecommitdiff
path: root/challenge-286
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-286')
-rw-r--r--challenge-286/lubos-kolouch/perl/ch-1.pl47
-rw-r--r--challenge-286/lubos-kolouch/perl/ch-2.pl68
-rw-r--r--challenge-286/lubos-kolouch/python/ch-1.py40
-rw-r--r--challenge-286/lubos-kolouch/python/ch-2.py64
4 files changed, 219 insertions, 0 deletions
diff --git a/challenge-286/lubos-kolouch/perl/ch-1.pl b/challenge-286/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..cf14677262
--- /dev/null
+++ b/challenge-286/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,47 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use File::Basename;
+use List::Util 'shuffle';
+use Test::More tests => 1;
+
+=pod
+
+=head1 DESCRIPTION
+
+This script reads its own source code, splits it into words (anything between whitespace), selects one at random, and prints it.
+
+=head1 FUNCTIONS
+
+=head2 get_random_word()
+
+Reads the script's own source code and returns a random word from it.
+
+Returns: A random word as a string.
+
+=cut
+
+sub get_random_word {
+ my $filename = $0; # $0 contains the name of the script
+ open my $fh, '<', $filename or die "Cannot open $filename: $!";
+ my @words;
+ while (my $line = <$fh>) {
+ push @words, split(/\s+/, $line);
+ }
+ close $fh;
+ my $random_word = $words[ rand @words ];
+ return $random_word;
+}
+
+# Main execution
+my $word = get_random_word();
+print "$word\n";
+
+# Test to ensure the word is from the source code
+subtest 'Test if word is from source code' => sub {
+ my $filename = $0;
+ open my $fh, '<', $filename or die "Cannot open $filename: $!";
+ my $content = do { local $/; <$fh> };
+ close $fh;
+ like($content, qr/\b\Q$word\E\b/, 'Word is from source code');
+};
diff --git a/challenge-286/lubos-kolouch/perl/ch-2.pl b/challenge-286/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..646607c2fe
--- /dev/null
+++ b/challenge-286/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,68 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use Test::More tests => 3;
+
+=pod
+
+=head1 DESCRIPTION
+
+This script simulates the Order Game on a given array of integers whose length is a power of 2.
+
+In the Order Game:
+
+- At each round, the array is reduced by pairing elements.
+- For each pair, if it's at an even index (starting from 0), take the minimum of the pair.
+- If it's at an odd index, take the maximum of the pair.
+- This process repeats until only one element remains.
+
+=head1 FUNCTIONS
+
+=head2 order_game(\@ints)
+
+Simulates the Order Game and returns the last remaining element.
+
+=over 4
+
+=item * C<\@ints> - Reference to an array of integers (length is a power of 2).
+
+=back
+
+Returns the last remaining integer after playing the game.
+
+=cut
+
+sub order_game {
+ my ($ints_ref) = @_;
+ my @current_list = @$ints_ref;
+ while (@current_list > 1) {
+ my @next_list;
+ my $n = scalar @current_list;
+ for (my $i = 0; $i < $n / 2; $i++) {
+ if ($i % 2 == 0) {
+ # Even index: take minimum
+ push @next_list, min($current_list[2 * $i], $current_list[2 * $i + 1]);
+ } else {
+ # Odd index: take maximum
+ push @next_list, max($current_list[2 * $i], $current_list[2 * $i + 1]);
+ }
+ }
+ @current_list = @next_list;
+ }
+ return $current_list[0];
+}
+
+sub min {
+ my ($a, $b) = @_;
+ return $a < $b ? $a : $b;
+}
+
+sub max {
+ my ($a, $b) = @_;
+ return $a > $b ? $a : $b;
+}
+
+# Unit Tests
+is(order_game([2, 1, 4, 5, 6, 3, 0, 2]), 1, 'Example 1');
+is(order_game([0, 5, 3, 2]), 0, 'Example 2');
+is(order_game([9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8]), 2, 'Example 3');
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()