aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-320/dave-jacoby/perl/ch-1.pl27
-rw-r--r--challenge-320/dave-jacoby/perl/ch-2.pl30
2 files changed, 57 insertions, 0 deletions
diff --git a/challenge-320/dave-jacoby/perl/ch-1.pl b/challenge-320/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..052d35c437
--- /dev/null
+++ b/challenge-320/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say state postderef signatures };
+
+my @examples = (
+
+ [ -3, -2, -1, 1, 2, 3 ],
+ [ -2, -1, 0, 0, 1 ],
+ [ 1, 2, 3, 4 ],
+);
+
+for my $example (@examples) {
+ my $str = join ', ', $example->@*;
+ my $output = max_count( $example->@* );
+ say <<"END";
+ Input: \$str = ($str)
+ Output: $output
+END
+}
+
+sub max_count(@ints) {
+ my $pos = scalar grep { $_ > 0 } @ints;
+ my $neg = scalar grep { $_ < 0 } @ints;
+ return $pos > $neg ? $pos : $neg;
+}
diff --git a/challenge-320/dave-jacoby/perl/ch-2.pl b/challenge-320/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..f5816533d5
--- /dev/null
+++ b/challenge-320/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say state postderef signatures };
+
+use List::Util qw{ sum0 };
+
+my @examples = (
+
+ [ 1, 23, 4, 5 ],
+ [ 1, 2, 3, 4, 5 ],
+ [ 1, 2, 34 ],
+);
+
+for my $example (@examples) {
+ my $str = join ', ', $example->@*;
+ my $output = sum_diff( $example->@* );
+ say <<"END";
+ Input: \$str = ($str)
+ Output: $output
+END
+}
+
+sub sum_diff (@ints) {
+ my $digit_sum = sum0 @ints;
+ my $element_sum = sum0 map { split //, $_ } @ints;
+ return abs $digit_sum - $element_sum;
+}
+