aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-05-14 13:46:48 +0100
committerGitHub <noreply@github.com>2024-05-14 13:46:48 +0100
commit7e36a15912b15ee4b27473e3f0cb2f5bf704007c (patch)
tree5e893053dc632a5c9c226d59c2e012c66776b923
parentf53201f3848484c23aa2e8753d4c0c0238553e8f (diff)
parent75303738ba77ae1169e1be3a64250a1ad5e60035 (diff)
downloadperlweeklychallenge-club-7e36a15912b15ee4b27473e3f0cb2f5bf704007c.tar.gz
perlweeklychallenge-club-7e36a15912b15ee4b27473e3f0cb2f5bf704007c.tar.bz2
perlweeklychallenge-club-7e36a15912b15ee4b27473e3f0cb2f5bf704007c.zip
Merge pull request #10089 from pme/challenge-269
challenge-269
-rwxr-xr-xchallenge-269/peter-meszaros/perl/ch-1.pl65
-rwxr-xr-xchallenge-269/peter-meszaros/perl/ch-2.pl134
2 files changed, 199 insertions, 0 deletions
diff --git a/challenge-269/peter-meszaros/perl/ch-1.pl b/challenge-269/peter-meszaros/perl/ch-1.pl
new file mode 100755
index 0000000000..a881293f34
--- /dev/null
+++ b/challenge-269/peter-meszaros/perl/ch-1.pl
@@ -0,0 +1,65 @@
+#!/usr/bin/env perl
+#
+=head1 Task 1: Bitwise OR
+
+You are given an array of positive integers, @ints.
+
+Write a script to find out if it is possible to select two or more elements of
+the given array such that the bitwise OR of the selected elements has atlest
+one trailing zero in its binary representation.
+
+=head2 Example 1
+
+ Input: @ints = (1, 2, 3, 4, 5)
+ Output: true
+
+Say, we pick 2 and 4, thier bitwise OR is 6. The binary representation of 6 is 110.
+Return true since we have one trailing zero.
+
+=head2 Example 2
+
+ Input: @ints = (2, 3, 8, 16)
+ Output: true
+
+Say, we pick 2 and 8, thier bitwise OR is 10. The binary representation of 10 is 1010.
+Return true since we have one trailing zero.
+
+=head2 Example 3
+
+ Input: @ints = (1, 2, 5, 7, 9)
+ Output: false
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ [[1, 2, 3, 4, 5], 1],
+ [[2, 3, 8, 16], 1],
+ [[1, 2, 5, 7, 9], 0],
+];
+
+sub bitwise_or
+{
+ my $l = shift;
+
+ my $c;
+ for my $n (@$l) {
+ unless ($n % 2) {
+ return 1 if $c;
+ ++$c;
+ }
+ }
+ return 0;
+}
+
+for (@$cases) {
+ is(bitwise_or($_->[0]), $_->[1], $_->[2]);
+}
+
+done_testing();
+
+exit 0;
diff --git a/challenge-269/peter-meszaros/perl/ch-2.pl b/challenge-269/peter-meszaros/perl/ch-2.pl
new file mode 100755
index 0000000000..8638fd4869
--- /dev/null
+++ b/challenge-269/peter-meszaros/perl/ch-2.pl
@@ -0,0 +1,134 @@
+#!/usr/bin/env perl
+#
+=head1 Task 2: Distribute Elements
+
+You are given an array of distinct integers, @ints.
+
+Write a script to distribute the elements as described below:
+
+1) Put the 1st element of the given array to a new array @arr1.
+2) Put the 2nd element of the given array to a new array @arr2.
+
+Once you have one element in each arrays, @arr1 and @arr2, then follow the rule
+below:
+
+If the last element of the array @arr1 is greater than the last
+element of the array @arr2 then add the first element of the
+given array to @arr1 otherwise to the array @arr2.
+
+When done distribution, return the concatenated arrays. @arr1 and @arr2.
+
+=head2 Example 1
+
+ Input: @ints = (2, 1, 3, 4, 5)
+ Output: (2, 3, 4, 5, 1)
+
+ 1st operation:
+ Add 1 to @arr1 = (2)
+
+ 2nd operation:
+ Add 2 to @arr2 = (1)
+
+ 3rd operation:
+ Now the last element of @arr1 is greater than the last element
+ of @arr2, add 3 to @arr1 = (2, 3).
+
+ 4th operation:
+ Again the last element of @arr1 is greate than the last element
+ of @arr2, add 4 to @arr1 = (2, 3, 4)
+
+ 5th operation:
+ Finally, the last element of @arr1 is again greater than the last
+ element of @arr2, add 5 to @arr1 = (2, 3, 4, 5)
+
+ Mow we have two arrays:
+ @arr1 = (2, 3, 4, 5)
+ @arr2 = (1)
+
+Concatenate the two arrays and return the final array: (2, 3, 4, 5, 1).
+
+=head2 Example 2
+
+ Input: @ints = (3, 2, 4)
+ Output: (3, 4, 2)
+
+ 1st operation:
+ Add 1 to @arr1 = (3)
+
+ 2nd operation:
+ Add 2 to @arr2 = (2)
+
+ 3rd operation:
+ Now the last element of @arr1 is greater than the last element
+ of @arr2, add 4 to @arr1 = (3, 4).
+
+ Mow we have two arrays:
+ @arr1 = (3, 4)
+ @arr2 = (2)
+
+Concatenate the two arrays and return the final array: (3, 4, 2).
+
+=head2 Example 3
+
+ Input: @ints = (5, 4, 3 ,8)
+ Output: (5, 3, 4, 8)
+
+ 1st operation:
+ Add 1 to @arr1 = (5)
+
+ 2nd operation:
+ Add 2 to @arr2 = (4)
+
+ 3rd operation:
+ Now the last element of @arr1 is greater than the last element
+ of @arr2, add 3 to @arr1 = (5, 3).
+
+ 4th operation:
+ Again the last element of @arr2 is greate than the last element
+ of @arr1, add 8 to @arr2 = (4, 8)
+
+ Mow we have two arrays:
+ @arr1 = (5, 3)
+ @arr2 = (4, 8)
+
+Concatenate the two arrays and return the final array: (5, 3, 4, 8).
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ [[2, 1, 3, 4, 5], [2, 3, 4, 5, 1]],
+ [[3, 2, 4], [3, 4, 2]],
+ [[5, 4, 3 ,8], [5, 3, 4, 8]],
+];
+
+sub distribute_elements
+{
+ my $l = shift;
+
+ my (@arr1, @arr2);
+ $arr1[0] = shift @$l;
+ $arr2[0] = shift @$l;
+
+ for my $e (@$l) {
+ if ($arr1[-1] > $arr2[-1]) {
+ push @arr1, $e;
+ } else {
+ push @arr2, $e;
+ }
+ }
+
+ return [@arr1, @arr2];
+}
+
+for (@$cases) {
+ is(distribute_elements($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
+