aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiels van Dijke <perlboy@cpan.org>2024-05-13 20:21:34 +0000
committerNiels van Dijke <perlboy@cpan.org>2024-05-13 20:21:34 +0000
commit5b819ae223242e37d04cee131ba2bc1860a7b81e (patch)
tree28ad6f112e93fbbc52c8d0300444ae4aa62c40b1
parent02e309b3d451fff60404eb5ab3e539056f99ce0d (diff)
downloadperlweeklychallenge-club-5b819ae223242e37d04cee131ba2bc1860a7b81e.tar.gz
perlweeklychallenge-club-5b819ae223242e37d04cee131ba2bc1860a7b81e.tar.bz2
perlweeklychallenge-club-5b819ae223242e37d04cee131ba2bc1860a7b81e.zip
w269 - Task 1 & 2
-rwxr-xr-xchallenge-269/perlboy1967/perl/ch1.pl40
-rwxr-xr-xchallenge-269/perlboy1967/perl/ch2.pl52
2 files changed, 92 insertions, 0 deletions
diff --git a/challenge-269/perlboy1967/perl/ch1.pl b/challenge-269/perlboy1967/perl/ch1.pl
new file mode 100755
index 0000000000..a35c6e2afe
--- /dev/null
+++ b/challenge-269/perlboy1967/perl/ch1.pl
@@ -0,0 +1,40 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 269
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-269
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 1: Bitwise OR
+Submitted by: Mohammad Sajid Anwar
+
+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.
+
+=cut
+
+use v5.32;
+use feature qw(signatures);
+use common::sense;
+
+use Test2::V0;
+
+sub bitwiseOr (@ints) {
+ my $c = 0;
+ for (@ints) {
+ $c++ if ($_ & 1) == 0;
+ return 1 if $c > 1;
+ }
+ return 0;
+}
+
+is(bitwiseOr(1,2,3,4,5),1,'Example 1.1');
+is(bitwiseOr(2,3,8,16),1,'Example 1.2');
+is(bitwiseOr(1,2,5,7,9),0,'Example 1.3');
+
+done_testing;
diff --git a/challenge-269/perlboy1967/perl/ch2.pl b/challenge-269/perlboy1967/perl/ch2.pl
new file mode 100755
index 0000000000..f1bc72b785
--- /dev/null
+++ b/challenge-269/perlboy1967/perl/ch2.pl
@@ -0,0 +1,52 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 269
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-269
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: Distribute Elements
+Submitted by: Mohammad Sajid Anwar
+
+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.
+
+=cut
+
+use v5.32;
+use feature qw(signatures);
+use common::sense;
+
+use Test2::V0;
+
+sub distributeElements (@ints) {
+ my @a = shift @ints; my @b = shift @ints;
+ for (@ints) {
+ $a[-1] < $b[-1] ? push(@b,$_) : push(@a,$_);
+ }
+ return (@a,@b);
+}
+
+is([distributeElements(2,1,3,4,5)],
+ [2,3,4,5,1],'Example 2.1');
+is([distributeElements(3,2,4)],
+ [3,4,2],'Example 2.2');
+is([distributeElements(5,4,3,8)],
+ [5,3,4,8],'Example 2.3');
+is([distributeElements(3,2,2,1)],
+ [3,2,1,2],'Own test');
+done_testing;