diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-05-14 13:48:38 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-14 13:48:38 +0100 |
| commit | 00c4ca8130102d9c3b8762fa894d30c546f6df6b (patch) | |
| tree | 6f073950ae309dab1e6b4a668b2758849f05c0e9 | |
| parent | 563af285222bad87c8e7387e25544f0b64e696b8 (diff) | |
| parent | 5b819ae223242e37d04cee131ba2bc1860a7b81e (diff) | |
| download | perlweeklychallenge-club-00c4ca8130102d9c3b8762fa894d30c546f6df6b.tar.gz perlweeklychallenge-club-00c4ca8130102d9c3b8762fa894d30c546f6df6b.tar.bz2 perlweeklychallenge-club-00c4ca8130102d9c3b8762fa894d30c546f6df6b.zip | |
Merge pull request #10092 from PerlBoy1967/branch-for-challenge-269
w269 - Task 1 & 2
| -rwxr-xr-x | challenge-269/perlboy1967/perl/ch1.pl | 40 | ||||
| -rwxr-xr-x | challenge-269/perlboy1967/perl/ch2.pl | 52 |
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; |
