aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiels van Dijke <perlboy@cpan.org>2024-04-08 21:49:54 +0000
committerNiels van Dijke <perlboy@cpan.org>2024-04-08 21:49:54 +0000
commit1b2f47c7ef3692bf73bb0b80e98ac78672aebd65 (patch)
treef5df6eff8806bc876be073f5c5df103cb4ce16c8
parentf14e99c374e42f272a7e43324e5ac195996c9a54 (diff)
downloadperlweeklychallenge-club-1b2f47c7ef3692bf73bb0b80e98ac78672aebd65.tar.gz
perlweeklychallenge-club-1b2f47c7ef3692bf73bb0b80e98ac78672aebd65.tar.bz2
perlweeklychallenge-club-1b2f47c7ef3692bf73bb0b80e98ac78672aebd65.zip
w262 - Task 1 & 2 (oops forgotten to push)
-rwxr-xr-xchallenge-262/perlboy1967/perl/ch1.pl44
-rwxr-xr-xchallenge-262/perlboy1967/perl/ch2.pl41
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;