From 3d46e47ea17c6f44cec49f9009f292cb6ff3c159 Mon Sep 17 00:00:00 2001 From: Andreas Mahnke Date: Tue, 6 May 2025 22:40:46 +0200 Subject: Challenge 320 --- challenge-320/mahnkong/perl/ch-1.pl | 19 +++++++++++++++++++ challenge-320/mahnkong/perl/ch-2.pl | 22 ++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 challenge-320/mahnkong/perl/ch-1.pl create mode 100644 challenge-320/mahnkong/perl/ch-2.pl 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"); -- cgit