aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-05-06 21:45:13 +0100
committerGitHub <noreply@github.com>2025-05-06 21:45:13 +0100
commitd9d6703ef6a21716d6a1137239ba54a24ba71b5c (patch)
tree9a3c3d62075cb1ed420cad4eddcbcebe2946395c
parente5f8174b2dae605f3229c39a99e62cb11a9dfd04 (diff)
parent3d46e47ea17c6f44cec49f9009f292cb6ff3c159 (diff)
downloadperlweeklychallenge-club-d9d6703ef6a21716d6a1137239ba54a24ba71b5c.tar.gz
perlweeklychallenge-club-d9d6703ef6a21716d6a1137239ba54a24ba71b5c.tar.bz2
perlweeklychallenge-club-d9d6703ef6a21716d6a1137239ba54a24ba71b5c.zip
Merge pull request #11991 from mahnkong/challenge-320
Challenge 320
-rw-r--r--challenge-320/mahnkong/perl/ch-1.pl19
-rw-r--r--challenge-320/mahnkong/perl/ch-2.pl22
2 files changed, 41 insertions, 0 deletions
diff --git a/challenge-320/mahnkong/perl/ch-1.pl b/challenge-320/mahnkong/perl/ch-1.pl
new file mode 100644
index 0000000000..4622d42ee6
--- /dev/null
+++ b/challenge-320/mahnkong/perl/ch-1.pl
@@ -0,0 +1,19 @@
+use strict;
+use warnings;
+use feature 'signatures';
+use Test::More 'no_plan';
+
+sub run(@ints) {
+ my $positive = 0;
+ my $negative = 0;
+
+ foreach my $i (@ints) {
+ $positive++ if $i > 0;
+ $negative++ if $i < 0;
+ }
+ return $positive > $negative ? $positive : $negative;
+}
+
+is(run(-3, -2, -1, 1, 2, 3), 3, "Example 1");
+is(run(-2, -1, 0, 0, 1), 2, "Example 2");
+is(run(1, 2, 3, 4), 4, "Example 3");
diff --git a/challenge-320/mahnkong/perl/ch-2.pl b/challenge-320/mahnkong/perl/ch-2.pl
new file mode 100644
index 0000000000..3db66e430d
--- /dev/null
+++ b/challenge-320/mahnkong/perl/ch-2.pl
@@ -0,0 +1,22 @@
+use strict;
+use warnings;
+use feature 'signatures';
+use Test::More 'no_plan';
+
+sub run(@ints) {
+ my $element_sum = 0;
+ my $digit_sum = 0;
+
+ foreach my $i (@ints) {
+ $element_sum += $i;
+ foreach my $p (split //, $i) {
+ $digit_sum += $p;
+ }
+ }
+
+ return abs($element_sum - $digit_sum);
+}
+
+is(run(1, 23, 4, 5), 18, "Example 1");
+is(run(1, 2, 3, 4, 5), 0, "Example 2");
+is(run(1, 2, 34), 27, "Example 3");