aboutsummaryrefslogtreecommitdiff
path: root/challenge-262
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-04-07 10:32:16 +0100
committerGitHub <noreply@github.com>2024-04-07 10:32:16 +0100
commit3a3ae93f3d08fdf3b2a057ccd71f4f4fd7135639 (patch)
tree57bf24be929e618cb3fb5f54ac4971871dc8c438 /challenge-262
parente484d7ac4c840628053e288060c17756e1206d28 (diff)
parentab1954ed13c61302b7290119c3086326148f70db (diff)
downloadperlweeklychallenge-club-3a3ae93f3d08fdf3b2a057ccd71f4f4fd7135639.tar.gz
perlweeklychallenge-club-3a3ae93f3d08fdf3b2a057ccd71f4f4fd7135639.tar.bz2
perlweeklychallenge-club-3a3ae93f3d08fdf3b2a057ccd71f4f4fd7135639.zip
Merge pull request #9880 from kjetillll/challenge-262-kjetillll
https://theweeklychallenge.org/blog/perl-weekly-challenge-262/
Diffstat (limited to 'challenge-262')
-rw-r--r--challenge-262/kjetillll/perl/ch-1.pl25
-rw-r--r--challenge-262/kjetillll/perl/ch-2.pl31
2 files changed, 56 insertions, 0 deletions
diff --git a/challenge-262/kjetillll/perl/ch-1.pl b/challenge-262/kjetillll/perl/ch-1.pl
new file mode 100644
index 0000000000..f82f7c9932
--- /dev/null
+++ b/challenge-262/kjetillll/perl/ch-1.pl
@@ -0,0 +1,25 @@
+use strict; use warnings;
+
+sub max_pos_neg {
+ my @count = (0,0);
+ $count[ $_ >= 0 ]++ for @_;
+ $count[ $count[0] < $count[1] ];
+}
+
+#========== Test ========================================
+use Test::More;
+is( max_pos_neg( @{ $$_{input} } ), $$_{output} )
+ for
+ {
+ input => [-3, 1, 2, -1, 3, -2, 4],
+ output => 4
+ },
+ {
+ input => [-1, -2, -3, 1],
+ output => 3
+ },
+ {
+ input => [1,2],
+ output => 2
+ };
+done_testing;
diff --git a/challenge-262/kjetillll/perl/ch-2.pl b/challenge-262/kjetillll/perl/ch-2.pl
new file mode 100644
index 0000000000..13d5792c3c
--- /dev/null
+++ b/challenge-262/kjetillll/perl/ch-2.pl
@@ -0,0 +1,31 @@
+use List::Util reduce; use strict; use warnings;
+sub cart{@{(reduce{[map{//;map[@$_,$'],@$a}@$b]}[[]],@_)}}
+
+sub eq_div_count {
+ my( $ints, $k ) = @_;
+ 0 + grep {
+ my( $i, $j ) = @$_;
+ 0 <= $i and
+ $i < $j and
+ $j < @$ints and
+ $$ints[ $i ] == $$ints[ $j ] and
+ ($i * $j) % $k == 0
+ }
+ cart( [ 0 .. @$ints-2],
+ [ 0 .. @$ints-1] )
+}
+
+#========== Test ========================================
+use Test::More;
+is( eq_div_count( @$_{'input','k'} ), $$_{output} ) for
+ {
+ input => [ 3, 1, 2, 2, 2, 1, 3 ],
+ k => 2,
+ output => 4
+ },
+ {
+ input => [ 1, 2, 3 ],
+ k => 1,
+ output => 0
+ };
+done_testing;