aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2024-05-14 13:48:33 +0200
committerLubos Kolouch <lubos@kolouch.net>2024-05-14 13:48:33 +0200
commit5c81232dbefd4753d61eb326d384a5b68bb10f07 (patch)
treef228899d997e2bfa2b1a47a0a9c3de5979ef20ff
parentcdf4c36b30c54acc0c3081f7901c5976213fc71d (diff)
downloadperlweeklychallenge-club-5c81232dbefd4753d61eb326d384a5b68bb10f07.tar.gz
perlweeklychallenge-club-5c81232dbefd4753d61eb326d384a5b68bb10f07.tar.bz2
perlweeklychallenge-club-5c81232dbefd4753d61eb326d384a5b68bb10f07.zip
Challenge 269 LK Perl Python
-rw-r--r--challenge-269/lubos-kolouch/perl/ch-1.pl26
-rw-r--r--challenge-269/lubos-kolouch/perl/ch-2.pl32
-rw-r--r--challenge-269/lubos-kolouch/python/ch-1.py43
-rw-r--r--challenge-269/lubos-kolouch/python/ch-2.py49
4 files changed, 150 insertions, 0 deletions
diff --git a/challenge-269/lubos-kolouch/perl/ch-1.pl b/challenge-269/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..48aaa62dac
--- /dev/null
+++ b/challenge-269/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,26 @@
+use strict;
+use warnings;
+use Test::More tests => 5;
+
+sub bitwise_or_with_trailing_zero {
+ my @ints = @_;
+ my $n = scalar @ints;
+
+ for my $i (0 .. $n - 2) {
+ for my $j ( $i + 1 .. $n - 1) {
+ if (($ints[$i] | $ints[$j]) % 2 == 0) {
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+
+# Unit tests
+is(bitwise_or_with_trailing_zero(1, 2, 3, 4, 5), 1, 'Example 1');
+is(bitwise_or_with_trailing_zero(2, 3, 8, 16), 1, 'Example 2');
+is(bitwise_or_with_trailing_zero(1, 2, 5, 7, 9), 0, 'Example 3');
+is(bitwise_or_with_trailing_zero(1, 3, 5, 7, 9), 0, 'No trailing zero');
+is(bitwise_or_with_trailing_zero(2, 4, 6, 8, 10), 1, 'All even');
+
+done_testing();
diff --git a/challenge-269/lubos-kolouch/perl/ch-2.pl b/challenge-269/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..4821aee6de
--- /dev/null
+++ b/challenge-269/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,32 @@
+use strict;
+use warnings;
+use Test::More tests => 3;
+
+sub distribute_elements {
+ my @ints = @_;
+ my @arr1;
+ my @arr2;
+
+ # Initial placement
+ push @arr1, shift @ints if @ints;
+ push @arr2, shift @ints if @ints;
+
+ # Distribute remaining elements
+ foreach my $elem (@ints) {
+ if ($arr1[-1] > $arr2[-1]) {
+ push @arr1, $elem;
+ } else {
+ push @arr2, $elem;
+ }
+ }
+
+ # Concatenate and return result
+ return (@arr1, @arr2);
+}
+
+# Unit tests
+is_deeply([distribute_elements(2, 1, 3, 4, 5)], [2, 3, 4, 5, 1], 'Example 1');
+is_deeply([distribute_elements(3, 2, 4)], [3, 4, 2], 'Example 2');
+is_deeply([distribute_elements(5, 4, 3, 8)], [5, 3, 4, 8], 'Example 3');
+
+done_testing();
diff --git a/challenge-269/lubos-kolouch/python/ch-1.py b/challenge-269/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..8e8f01d26e
--- /dev/null
+++ b/challenge-269/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,43 @@
+from typing import List
+import unittest
+
+
+def bitwise_or_with_trailing_zero(ints: List[int]) -> bool:
+ """
+ Determine if it's possible to select two or more elements from the list
+ such that their bitwise OR has at least one trailing zero.
+
+ Args:
+ ints (List[int]): The input list of positive integers.
+
+ Returns:
+ bool: True if such a selection is possible, False otherwise.
+ """
+ n = len(ints)
+ for i in range(n):
+ for j in range(i + 1, n):
+ if (ints[i] | ints[j]) % 2 == 0:
+ return True
+ return False
+
+
+# Unit tests
+class TestBitwiseOrWithTrailingZero(unittest.TestCase):
+ def test_example_1(self):
+ self.assertTrue(bitwise_or_with_trailing_zero([1, 2, 3, 4, 5]))
+
+ def test_example_2(self):
+ self.assertTrue(bitwise_or_with_trailing_zero([2, 3, 8, 16]))
+
+ def test_example_3(self):
+ self.assertFalse(bitwise_or_with_trailing_zero([1, 2, 5, 7, 9]))
+
+ def test_no_trailing_zero(self):
+ self.assertFalse(bitwise_or_with_trailing_zero([1, 3, 5, 7, 9]))
+
+ def test_all_even(self):
+ self.assertTrue(bitwise_or_with_trailing_zero([2, 4, 6, 8, 10]))
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/challenge-269/lubos-kolouch/python/ch-2.py b/challenge-269/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..b273d863ee
--- /dev/null
+++ b/challenge-269/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,49 @@
+from typing import List
+import unittest
+
+
+def distribute_elements(ints: List[int]) -> List[int]:
+ """
+ Distribute elements of the given list into two separate lists according to the specified rules
+ and return the concatenated result.
+
+ Args:
+ ints (List[int]): The input list of distinct integers.
+
+ Returns:
+ List[int]: The concatenated list of elements from the two distributed lists.
+ """
+ arr1 = []
+ arr2 = []
+
+ # Initial placement
+ if ints:
+ arr1.append(ints.pop(0))
+ if ints:
+ arr2.append(ints.pop(0))
+
+ # Distribute remaining elements
+ for elem in ints:
+ if arr1[-1] > arr2[-1]:
+ arr1.append(elem)
+ else:
+ arr2.append(elem)
+
+ # Concatenate and return result
+ return arr1 + arr2
+
+
+# Unit tests
+class TestDistributeElements(unittest.TestCase):
+ def test_example_1(self):
+ self.assertEqual(distribute_elements([2, 1, 3, 4, 5]), [2, 3, 4, 5, 1])
+
+ def test_example_2(self):
+ self.assertEqual(distribute_elements([3, 2, 4]), [3, 4, 2])
+
+ def test_example_3(self):
+ self.assertEqual(distribute_elements([5, 4, 3, 8]), [5, 3, 4, 8])
+
+
+if __name__ == "__main__":
+ unittest.main()