diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-05-14 13:57:25 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-14 13:57:25 +0100 |
| commit | 7fa75c11a751d16a4706c4e1c9448b6122d54d02 (patch) | |
| tree | 3e9a030a1eaa03aaf1c8b7974a40d59eefc1c23a | |
| parent | cfcfb002820dbd073a1874a40e469a3b5a21d362 (diff) | |
| parent | 5c81232dbefd4753d61eb326d384a5b68bb10f07 (diff) | |
| download | perlweeklychallenge-club-7fa75c11a751d16a4706c4e1c9448b6122d54d02.tar.gz perlweeklychallenge-club-7fa75c11a751d16a4706c4e1c9448b6122d54d02.tar.bz2 perlweeklychallenge-club-7fa75c11a751d16a4706c4e1c9448b6122d54d02.zip | |
Merge pull request #10096 from LubosKolouch/master
Challenges 267 268 269 LK Perl Python
| -rw-r--r-- | challenge-267/lubos-kolouch/perl/ch-1.pl | 28 | ||||
| -rw-r--r-- | challenge-267/lubos-kolouch/perl/ch-2.pl | 36 | ||||
| -rw-r--r-- | challenge-267/lubos-kolouch/python/ch-1.py | 33 | ||||
| -rw-r--r-- | challenge-267/lubos-kolouch/python/ch-2.py | 38 | ||||
| -rw-r--r-- | challenge-268/lubos-kolouch/perl/ch-1.pl | 68 | ||||
| -rw-r--r-- | challenge-268/lubos-kolouch/perl/ch-2.pl | 31 | ||||
| -rw-r--r-- | challenge-268/lubos-kolouch/python/ch-1.py | 72 | ||||
| -rw-r--r-- | challenge-268/lubos-kolouch/python/ch-2.py | 55 | ||||
| -rw-r--r-- | challenge-269/lubos-kolouch/perl/ch-1.pl | 26 | ||||
| -rw-r--r-- | challenge-269/lubos-kolouch/perl/ch-2.pl | 32 | ||||
| -rw-r--r-- | challenge-269/lubos-kolouch/python/ch-1.py | 43 | ||||
| -rw-r--r-- | challenge-269/lubos-kolouch/python/ch-2.py | 49 |
12 files changed, 511 insertions, 0 deletions
diff --git a/challenge-267/lubos-kolouch/perl/ch-1.pl b/challenge-267/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..c1b0111a7c --- /dev/null +++ b/challenge-267/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,28 @@ +use strict; +use warnings; + +sub product_sign { + my @ints = @_; + + my $negative_count = 0; + foreach my $value (@ints) { + return 0 if $value == 0; + $negative_count++ if $value < 0; + } + + return $negative_count % 2 == 0 ? 1 : -1; +} + +# Test cases +use Test::More; + +is(product_sign(-1, -2, -3, -4, 3, 2, 1), 1, 'Example 1'); +is(product_sign(1, 2, 0, -2, -1), 0, 'Example 2'); +is(product_sign(-1, -1, 1, -1, 2), -1, 'Example 3'); + +done_testing(); + +# Print results for visualization +print "Output Example 1: ", product_sign(-1, -2, -3, -4, 3, 2, 1), "\n"; +print "Output Example 2: ", product_sign(1, 2, 0, -2, -1), "\n"; +print "Output Example 3: ", product_sign(-1, -1, 1, -1, 2), "\n"; diff --git a/challenge-267/lubos-kolouch/perl/ch-2.pl b/challenge-267/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..ec1dabe9db --- /dev/null +++ b/challenge-267/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,36 @@ +#!/usr/bin/perl +use strict; +use warnings FATAL => 'all'; + + +sub line_counts { + my ($str, @widths) = @_; + + my $max_width = 100; + my $current_width = 0; + my $lines = 1; + + foreach my $char (split //, $str) { + my $char_width = $widths[ord($char) - ord('a')]; + if ($current_width + $char_width > $max_width) { + $lines++; + $current_width = $char_width; + } else { + $current_width += $char_width; + } + } + + return ($lines, $current_width); +} + +# Test cases +use Test::More; + +is_deeply([line_counts("abcdefghijklmnopqrstuvwxyz", (10) x 26)], [3, 60], 'Example 1'); +is_deeply([line_counts("bbbcccdddaaa", (4, 10, (10) x 24))], [2, 4], 'Example 2'); + +done_testing(); + +# Print results for visualization +print "Output Example 1: ", join(", ", line_counts("abcdefghijklmnopqrstuvwxyz", (10) x 26)), "\n"; +print "Output Example 2: ", join(", ", line_counts("bbbcccdddaaa", (4, 10, (10) x 24))), "\n"; diff --git a/challenge-267/lubos-kolouch/python/ch-1.py b/challenge-267/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..46a4fe093c --- /dev/null +++ b/challenge-267/lubos-kolouch/python/ch-1.py @@ -0,0 +1,33 @@ +""" Challenge 267 Task 1 LK Python""" + +from typing import List + + +def product_sign(ints: List[int]) -> int: + """ + Computes the sign of the product of a list of integers. + + Args: + ints (List[int]): A list of integers. + + Returns: + int: The sign of the product of the integers (1, -1, or 0). + """ + negative_count = 0 + for value in ints: + if value == 0: + return 0 + elif value < 0: + negative_count += 1 + + return -1 if negative_count % 2 != 0 else 1 + + +# Test cases +assert product_sign([-1, -2, -3, -4, 3, 2, 1]) == 1 +assert product_sign([1, 2, 0, -2, -1]) == 0 +assert product_sign([-1, -1, 1, -1, 2]) == -1 + +print(product_sign([-1, -2, -3, -4, 3, 2, 1])) # Output: 1 +print(product_sign([1, 2, 0, -2, -1])) # Output: 0 +print(product_sign([-1, -1, 1, -1, 2])) # Output: -1 diff --git a/challenge-267/lubos-kolouch/python/ch-2.py b/challenge-267/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..850f1ab505 --- /dev/null +++ b/challenge-267/lubos-kolouch/python/ch-2.py @@ -0,0 +1,38 @@ +""" Challenge 267 Task 2 LK Python""" + +from typing import Tuple, List + + +def line_counts(s: str, widths: List[int]) -> Tuple[int, int]: + """ + Calculates the number of lines and the width of the last line required to display a string. + + Args: + s (str): The input string consisting of lowercase English letters. + widths (List[int]): A list of 26 integers representing the width of each character from 'a' to 'z'. + + Returns: + Tuple[int, int]: A tuple containing the number of lines used and the width of the last line. + """ + max_width = 100 + current_width = 0 + lines = 1 + + for char in s: + char_width = widths[ord(char) - ord("a")] + if current_width + char_width > max_width: + lines += 1 + current_width = char_width + else: + current_width += char_width + + return lines, current_width + + +# Test cases +assert line_counts("abcdefghijklmnopqrstuvwxyz", [10] * 26) == (3, 60) +assert line_counts("bbbcccdddaaa", [4, 10] + [10] * 24) == (2, 4) + +# Print results for visualization +print(line_counts("abcdefghijklmnopqrstuvwxyz", [10] * 26)) # Output: (3, 60) +print(line_counts("bbbcccdddaaa", [4, 10] + [10] * 24)) # Output: (2, 4) 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() 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() |
