diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-04-09 10:53:31 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-09 10:53:31 +0100 |
| commit | 05ee1469570a89f7be61de240eac030a9e924be7 (patch) | |
| tree | 0dbcb4b9bad1458d0a8e5aa96ebd3eb25141faa3 | |
| parent | b208be7e37d87e9f6e8edfeb3e7a220da920ff81 (diff) | |
| parent | 1b2f47c7ef3692bf73bb0b80e98ac78672aebd65 (diff) | |
| download | perlweeklychallenge-club-05ee1469570a89f7be61de240eac030a9e924be7.tar.gz perlweeklychallenge-club-05ee1469570a89f7be61de240eac030a9e924be7.tar.bz2 perlweeklychallenge-club-05ee1469570a89f7be61de240eac030a9e924be7.zip | |
Merge pull request #9900 from PerlBoy1967/branch-for-challenge-262
w262 - Task 1 & 2 (oops forgotten to push)
| -rwxr-xr-x | challenge-262/perlboy1967/perl/ch1.pl | 44 | ||||
| -rwxr-xr-x | challenge-262/perlboy1967/perl/ch2.pl | 41 |
2 files changed, 85 insertions, 0 deletions
diff --git a/challenge-262/perlboy1967/perl/ch1.pl b/challenge-262/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..2aa45838aa --- /dev/null +++ b/challenge-262/perlboy1967/perl/ch1.pl @@ -0,0 +1,44 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 262 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-262 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Max Positive Negative +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers, @ints. + +Write a script to return the maximum number of either positive or +negative integers in the given array. + +=cut + +use v5.32; +use feature qw(signatures); +use common::sense; + +use Test2::V0; + +use List::AllUtils qw(max); + +sub maxPositiveNegative (@ints) { + my @m = (0,0); + + for (@ints) { + $m[0]++ if ($_ > 0); + $m[1]++ if ($_ < 0); + } + + return max(@m); +} + +is(maxPositiveNegative(-3, 1, 2, -1, 3, -2, 4),4); +is(maxPositiveNegative(-1,-2,-3,1),3); +is(maxPositiveNegative(1,2),2); +is(maxPositiveNegative(-1,0,1),1); + +done_testing; diff --git a/challenge-262/perlboy1967/perl/ch2.pl b/challenge-262/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..9db2c96130 --- /dev/null +++ b/challenge-262/perlboy1967/perl/ch2.pl @@ -0,0 +1,41 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 262 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-262 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Count Equal Divisible +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers, @ints and an integer $k. + +Write a script to return the number of pairs (i, j) where + +a) 0 <= i < j < size of @ints +b) ints[i] == ints[j] +c) i x j is divisible by k + +=cut + +use v5.32; +use feature qw(signatures); +use common::sense; + +use Test2::V0; + +use Algorithm::Combinatorics qw(combinations); + +sub countEqualDivisible ($k,@ints) { + scalar grep { + $ints[$$_[0]] == $ints[$$_[1]] and + ($$_[0] * $$_[1]) % $k == 0; + } combinations([0..$#ints],2); +} + +is(countEqualDivisible(2,3,1,2,2,2,1,3),4); +is(countEqualDivisible(1,1,2,3),0); + +done_testing; |
