aboutsummaryrefslogtreecommitdiff
path: root/challenge-262
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-03-28 16:51:32 +0000
committerGitHub <noreply@github.com>2024-03-28 16:51:32 +0000
commit0cb6839f6cf7f4131704f2c6eb5b9b21d58a2d36 (patch)
tree1a56d2982699f5ed576048dcae03c9e8d9f489c6 /challenge-262
parent69367c5b88102fabee631384228617b990bf2e60 (diff)
parentaf833727a9f4c76635c9b752b03a857f0d26ed2a (diff)
downloadperlweeklychallenge-club-0cb6839f6cf7f4131704f2c6eb5b9b21d58a2d36.tar.gz
perlweeklychallenge-club-0cb6839f6cf7f4131704f2c6eb5b9b21d58a2d36.tar.bz2
perlweeklychallenge-club-0cb6839f6cf7f4131704f2c6eb5b9b21d58a2d36.zip
Merge pull request #9827 from jacoby/master
DAJ 262
Diffstat (limited to 'challenge-262')
-rw-r--r--challenge-262/dave-jacoby/perl/ch-1.pl29
-rw-r--r--challenge-262/dave-jacoby/perl/ch-2.pl34
2 files changed, 63 insertions, 0 deletions
diff --git a/challenge-262/dave-jacoby/perl/ch-1.pl b/challenge-262/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..2caa4c1b3c
--- /dev/null
+++ b/challenge-262/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say postderef signatures state };
+
+my @examples = (
+
+ [ -3, 1, 2, -1, 3, -2, 4 ],
+ [ -1, -2, -3, 1 ],
+ [ 1, 2 ],
+
+);
+
+for my $example (@examples) {
+ my $output = max_pos_neg( $example->@* );
+ my @ints = $example->@*;
+ my $ints = join ',', @ints;
+ say <<"END";
+ Input: \@ints = ($ints)
+ Output: $output
+END
+}
+
+sub max_pos_neg (@ints) {
+ my $pos = scalar grep { $_ > 0 } @ints;
+ my $neg = scalar grep { $_ < 0 } @ints;
+ return $pos > $neg ? $pos : $neg;
+}
diff --git a/challenge-262/dave-jacoby/perl/ch-2.pl b/challenge-262/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..3be159219a
--- /dev/null
+++ b/challenge-262/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say postderef signatures state };
+
+my @examples = (
+
+ { ints => [ 3, 1, 2, 2, 2, 1, 3 ], k => 2 },
+ { ints => [ 1, 2, 3 ], k => 1 },
+);
+
+for my $example (@examples) {
+ my $k = $example->{k};
+ my @ints = $example->{ints}->@*;
+ my $output = count_equal_divisible( $k, @ints );
+ my $ints = join ',', @ints;
+ say <<"END";
+ Input: \@ints = ($ints) and \$k = $k
+ Output: $output
+END
+}
+
+sub count_equal_divisible ( $k, @ints ) {
+ my $output = 0;
+ for my $i ( 0 .. $#ints ) {
+ for my $j ( $i + 1 .. $#ints ) {
+ next unless $ints[$i] == $ints[$j];
+ next unless ( $i * $j ) % $k == 0;
+ $output++;
+ }
+ }
+ return $output;
+}