aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2024-05-14 13:33:22 +0200
committerLubos Kolouch <lubos@kolouch.net>2024-05-14 13:33:22 +0200
commitcdf4c36b30c54acc0c3081f7901c5976213fc71d (patch)
treefb6dd9ab4114e2e73cd72e1d49c98c09b2fa8867
parent163ab9b715b400bdd98699d70fd4b7fc52482d4e (diff)
downloadperlweeklychallenge-club-cdf4c36b30c54acc0c3081f7901c5976213fc71d.tar.gz
perlweeklychallenge-club-cdf4c36b30c54acc0c3081f7901c5976213fc71d.tar.bz2
perlweeklychallenge-club-cdf4c36b30c54acc0c3081f7901c5976213fc71d.zip
Challenge 268 LK Perl Python
-rw-r--r--challenge-268/lubos-kolouch/perl/ch-1.pl68
-rw-r--r--challenge-268/lubos-kolouch/perl/ch-2.pl31
-rw-r--r--challenge-268/lubos-kolouch/python/ch-1.py72
-rw-r--r--challenge-268/lubos-kolouch/python/ch-2.py55
4 files changed, 226 insertions, 0 deletions
diff --git a/challenge-268/lubos-kolouch/perl/ch-1.pl b/challenge-268/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..049c1162ed
--- /dev/null
+++ b/challenge-268/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,68 @@
+use strict;
+use warnings;
+
+# Function to check if two arrays contain the same elements
+sub arrays_are_equal {
+ my ($a_ref, $b_ref) = @_;
+ my @a = sort { $a <=> $b } @$a_ref;
+ my @b = sort { $a <=> $b } @$b_ref;
+
+ return 0 if @a != @b;
+ for my $i (0 .. $#a) {
+ return 0 if $a[$i] != $b[$i];
+ }
+ return 1;
+}
+
+# Function to find the magic number
+sub find_magic_number {
+ my ($x_ref, $y_ref) = @_;
+ my @x = @$x_ref;
+ my @y = @$y_ref;
+
+ # Print input arrays for debugging
+ print "Array x: (", join(", ", @x), ")\n";
+ print "Array y: (", join(", ", @y), ")\n";
+
+ # Check if the arrays are empty or have different sizes
+ if (!@x || !@y) {
+ die "One of the input arrays is empty";
+ }
+ if (@x != @y) {
+ die "Input arrays have different sizes";
+ }
+
+ # Calculate possible magic number candidates
+ my %candidates;
+ for my $i (0 .. $#x) {
+ for my $j (0 .. $#y) {
+ my $candidate = $y[$j] - $x[$i];
+ $candidates{$candidate}++;
+ }
+ }
+
+ # Test each candidate
+ for my $candidate (keys %candidates) {
+ my @transformed_x = map { $_ + $candidate } @x;
+ if (arrays_are_equal(\@transformed_x, \@y)) {
+ return $candidate;
+ }
+ }
+
+ die "No magic number found";
+}
+
+# Example 1
+my @x1 = (3, 7, 5);
+my @y1 = (9, 5, 7);
+print "Example 1: Magic number is ", find_magic_number(\@x1, \@y1), "\n";
+
+# Example 2
+my @x2 = (1, 2, 1);
+my @y2 = (5, 4, 4);
+print "Example 2: Magic number is ", find_magic_number(\@x2, \@y2), "\n";
+
+# Example 3
+my @x3 = (2);
+my @y3 = (5);
+print "Example 3: Magic number is ", find_magic_number(\@x3, \@y3), "\n";
diff --git a/challenge-268/lubos-kolouch/perl/ch-2.pl b/challenge-268/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..4ba3dba149
--- /dev/null
+++ b/challenge-268/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,31 @@
+use strict;
+use warnings;
+use List::Util qw(min);
+use Test::More tests => 6;
+
+sub number_game {
+ my (@ints) = @_;
+ my @result;
+
+ while (@ints) {
+ # Find the two smallest integers
+ @ints = sort { $a <=> $b } @ints;
+ my $min1 = shift @ints;
+ my $min2 = shift @ints;
+
+ # Add them to the result array in decreasing order
+ push @result, ($min2, $min1);
+ }
+
+ return @result;
+}
+
+# Unit tests
+is_deeply([number_game(2, 5, 3, 4)], [3, 2, 5, 4], 'Example 1');
+is_deeply([number_game(9, 4, 1, 3, 6, 4, 6, 1)], [1, 1, 4, 3, 6, 4, 9, 6], 'Example 2');
+is_deeply([number_game(1, 2, 2, 3)], [2, 1, 3, 2], 'Example 3');
+is_deeply([number_game(1, 2, 3, 4, 5, 6)], [2, 1, 4, 3, 6, 5], 'Already sorted');
+is_deeply([number_game(6, 5, 4, 3, 2, 1)], [2, 1, 4, 3, 6, 5], 'Reverse sorted');
+is_deeply([number_game(5, 1, 5, 1, 2, 2)], [1, 1, 2, 2, 5, 5], 'Duplicate elements');
+
+done_testing();
diff --git a/challenge-268/lubos-kolouch/python/ch-1.py b/challenge-268/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..f1cdf322ed
--- /dev/null
+++ b/challenge-268/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,72 @@
+""" Challenge 268 LK Task 1 Python """
+
+from typing import List
+import unittest
+
+# Type definition for clarity
+IntList = List[int]
+
+
+def arrays_are_equal(a: IntList, b: IntList) -> bool:
+ """Check if two lists contain the same elements in any order."""
+ return sorted(a) == sorted(b)
+
+
+def find_magic_number(x: IntList, y: IntList) -> int:
+ """
+ Find the magic number that, when added to each element of x, gives the elements of y.
+
+ Args:
+ x (IntList): The first list of integers.
+ y (IntList): The second list of integers.
+
+ Returns:
+ int: The magic number.
+
+ Raises:
+ ValueError: If no magic number is found or input arrays are invalid.
+ """
+ if len(x) != len(y):
+ raise ValueError("Input arrays must be of the same size")
+ if not x or not y:
+ raise ValueError("Input arrays must not be empty")
+
+ possible_candidates = set()
+ for i in range(len(x)):
+ for j in range(len(y)):
+ possible_candidates.add(y[j] - x[i])
+
+ for candidate in possible_candidates:
+ transformed_x = [xi + candidate for xi in x]
+ if arrays_are_equal(transformed_x, y):
+ return candidate
+
+ raise ValueError("No magic number found")
+
+
+# Unit tests
+class TestMagicNumber(unittest.TestCase):
+ def test_example_1(self):
+ self.assertEqual(find_magic_number([3, 7, 5], [9, 5, 7]), 2)
+
+ def test_example_2(self):
+ self.assertEqual(find_magic_number([1, 2, 1], [5, 4, 4]), 3)
+
+ def test_example_3(self):
+ self.assertEqual(find_magic_number([2], [5]), 3)
+
+ def test_different_size(self):
+ with self.assertRaises(ValueError):
+ find_magic_number([1, 2, 3], [4, 5])
+
+ def test_empty_arrays(self):
+ with self.assertRaises(ValueError):
+ find_magic_number([], [])
+
+ def test_no_magic_number(self):
+ with self.assertRaises(ValueError):
+ find_magic_number([1, 2, 3], [10, 20, 30])
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/challenge-268/lubos-kolouch/python/ch-2.py b/challenge-268/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..f2a8c02cac
--- /dev/null
+++ b/challenge-268/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,55 @@
+""" Challenge 268 LK Task 2 Python """
+
+from typing import List
+import unittest
+
+
+def number_game(ints: List[int]) -> List[int]:
+ """
+ Create a new array by repeatedly picking the two smallest integers
+ from the input array and adding them to the new array in decreasing order.
+
+ Args:
+ ints (List[int]): The input list of integers.
+
+ Returns:
+ List[int]: The new list with elements added in the required order.
+ """
+ result = []
+ while ints:
+ # Find the two smallest integers
+ min1 = min(ints)
+ ints.remove(min1)
+ min2 = min(ints)
+ ints.remove(min2)
+
+ # Add them to the result array in decreasing order
+ result.extend([min2, min1])
+ return result
+
+
+# Unit tests
+class TestNumberGame(unittest.TestCase):
+ def test_example_1(self):
+ self.assertEqual(number_game([2, 5, 3, 4]), [3, 2, 5, 4])
+
+ def test_example_2(self):
+ self.assertEqual(
+ number_game([9, 4, 1, 3, 6, 4, 6, 1]), [1, 1, 4, 3, 6, 4, 9, 6]
+ )
+
+ def test_example_3(self):
+ self.assertEqual(number_game([1, 2, 2, 3]), [2, 1, 3, 2])
+
+ def test_already_sorted(self):
+ self.assertEqual(number_game([1, 2, 3, 4, 5, 6]), [2, 1, 4, 3, 6, 5])
+
+ def test_reverse_sorted(self):
+ self.assertEqual(number_game([6, 5, 4, 3, 2, 1]), [2, 1, 4, 3, 6, 5])
+
+ def test_duplicate_elements(self):
+ self.assertEqual(number_game([5, 1, 5, 1, 2, 2]), [1, 1, 2, 2, 5, 5])
+
+
+if __name__ == "__main__":
+ unittest.main()