aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Mahnke <andreas.mahnke@leuphana.de>2025-06-10 10:17:54 +0200
committerAndreas Mahnke <andreas.mahnke@leuphana.de>2025-06-10 10:17:54 +0200
commit9acb23e58a8bdc269167a82e3c44ddf1de60fbff (patch)
treeda513627799e7999735a2c7e3c4b3c7411321724
parente28477d2418099cfbb1a227133c69ddef6eed741 (diff)
downloadperlweeklychallenge-club-9acb23e58a8bdc269167a82e3c44ddf1de60fbff.tar.gz
perlweeklychallenge-club-9acb23e58a8bdc269167a82e3c44ddf1de60fbff.tar.bz2
perlweeklychallenge-club-9acb23e58a8bdc269167a82e3c44ddf1de60fbff.zip
Challenge 325
-rw-r--r--challenge-325/mahnkong/perl/ch-1.pl21
-rw-r--r--challenge-325/mahnkong/perl/ch-2.pl25
2 files changed, 46 insertions, 0 deletions
diff --git a/challenge-325/mahnkong/perl/ch-1.pl b/challenge-325/mahnkong/perl/ch-1.pl
new file mode 100644
index 0000000000..47773f762f
--- /dev/null
+++ b/challenge-325/mahnkong/perl/ch-1.pl
@@ -0,0 +1,21 @@
+use strict;
+use warnings;
+use feature 'signatures';
+use Test::More 'no_plan';
+
+sub run(@binary) {
+ my $max = 0;
+ my $current = 0;
+ for (my $i = 0; $i <= $#binary; $i++) {
+ $current += 1 if ($binary[$i] == 1);
+ if ($binary[$i] == 0 || $i == $#binary) {
+ $max = $current if $current > $max;
+ $current = 0;
+ }
+ }
+ return $max;
+}
+
+is(run(0, 1, 1, 0, 1, 1, 1), 3, "Example 1");
+is(run(0, 0, 0, 0), 0, "Example 2");
+is(run(1, 0, 1, 0, 1, 1), 2, "Example 3");
diff --git a/challenge-325/mahnkong/perl/ch-2.pl b/challenge-325/mahnkong/perl/ch-2.pl
new file mode 100644
index 0000000000..d79190e4d9
--- /dev/null
+++ b/challenge-325/mahnkong/perl/ch-2.pl
@@ -0,0 +1,25 @@
+use strict;
+use warnings;
+use feature 'signatures';
+use Test::More 'no_plan';
+
+sub run(@prices) {
+ my @result;
+
+ for (my $i = 0; $i < $#prices; $i++) {
+ my $price = $prices[$i];
+ for (my $j = $i + 1; $j <= $#prices; $j++) {
+ if ($prices[$j] <= $price) {
+ $price = $price - $prices[$j];
+ last;
+ }
+ }
+ push @result, $price;
+ }
+
+ return [@result, $prices[-1]];
+}
+
+is_deeply(run(8, 4, 6, 2, 3), [4, 2, 4, 2, 3], "Example 1");
+is_deeply(run(1, 2, 3, 4, 5), [1, 2, 3, 4, 5], "Example 2");
+is_deeply(run(7, 1, 1, 5), [6, 0, 1, 5], "Example 3");