aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-05-16 11:03:46 +0100
committerGitHub <noreply@github.com>2024-05-16 11:03:46 +0100
commitf526ea3ed759332c9b32699353056f3a6a10fa03 (patch)
tree7bd21a101f1b31eac309f1fc3bf4cbb3eb4b8d7c
parentb201b1fc4ad24f0d1db542484097fce6cc38fe3a (diff)
parent11008384434809cbb0e73455e0a2ed99b30ad63c (diff)
downloadperlweeklychallenge-club-f526ea3ed759332c9b32699353056f3a6a10fa03.tar.gz
perlweeklychallenge-club-f526ea3ed759332c9b32699353056f3a6a10fa03.tar.bz2
perlweeklychallenge-club-f526ea3ed759332c9b32699353056f3a6a10fa03.zip
Merge pull request #10104 from jeanluc2020/jeanluc-269
Add solution 269.
-rw-r--r--challenge-269/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-269/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-269/jeanluc2020/perl/ch-1.pl64
-rwxr-xr-xchallenge-269/jeanluc2020/perl/ch-2.pl129
4 files changed, 195 insertions, 0 deletions
diff --git a/challenge-269/jeanluc2020/blog-1.txt b/challenge-269/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..6d1f908a97
--- /dev/null
+++ b/challenge-269/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-269-1.html
diff --git a/challenge-269/jeanluc2020/blog-2.txt b/challenge-269/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..3db9f465a4
--- /dev/null
+++ b/challenge-269/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-269-2.html
diff --git a/challenge-269/jeanluc2020/perl/ch-1.pl b/challenge-269/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..3ce337b272
--- /dev/null
+++ b/challenge-269/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,64 @@
+#!/usr/bin/perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-269/#TASK1
+#
+# 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.
+#
+## 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.
+#
+## 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.
+#
+## Example 3
+##
+## Input: @ints = (1, 2, 5, 7, 9)
+## Output: false
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# The bitwise OR of two or more numbers has a trailing 0 if all numbers
+# have a trailing 0. This is the case if these numbers are even. So the
+# problem boils down to the question whether or not the array has at least
+# two even numbers.
+
+use strict;
+use warnings;
+
+bitwise_or(1, 2, 3, 4, 5);
+bitwise_or(2, 3, 8, 16);
+bitwise_or(1, 2, 5, 7, 9);
+
+sub bitwise_or {
+ my @ints = @_;
+ my $found_even = 0;
+ print "Input: (", join(", ", @ints), ")\n";
+ foreach my $int (@ints) {
+ $found_even++ unless $int % 2;
+ }
+ if($found_even >= 2) {
+ print "Output: true\n";
+ } else {
+ print "Output: false\n";
+ }
+}
diff --git a/challenge-269/jeanluc2020/perl/ch-2.pl b/challenge-269/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..57914de7ba
--- /dev/null
+++ b/challenge-269/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,129 @@
+#!/usr/bin/perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-269/#TASK2
+#
+# 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.
+#
+## 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).
+#
+## 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).
+#
+## 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).
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Not much to do than to implement the given rules.
+
+use strict;
+use warnings;
+
+distribute_elements(2, 1, 3, 4, 5);
+distribute_elements(3, 2, 4);
+distribute_elements(5, 4, 3 ,8);
+
+sub distribute_elements {
+ my @ints = @_;
+ my (@arr1, @arr2);
+ print "Input: (", join(", ", @ints), ")\n";
+ if($#ints <= 1) {
+ return print "Output: (", join(", ", @ints), ")\n";
+ }
+ ($arr1[0], $arr2[0], @ints) = @ints;
+ while(@ints) {
+ if($arr1[-1] > $arr2[-1]) {
+ push @arr1, shift @ints;
+ } else {
+ push @arr2, shift @ints;
+ }
+ }
+ @ints = (@arr1, @arr2);
+ print "Output: (", join(", ", @ints), ")\n";
+}