diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-06-10 09:25:12 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-10 09:25:12 +0100 |
| commit | 35a38c808be437c6404f0a02c3c5308597e02255 (patch) | |
| tree | e45a441168d04a139594ce4b40f8e1b68873ebec /challenge-325 | |
| parent | a0bc8dcb2619e6ce543b1aea51628cf11a6524ac (diff) | |
| parent | 9acb23e58a8bdc269167a82e3c44ddf1de60fbff (diff) | |
| download | perlweeklychallenge-club-35a38c808be437c6404f0a02c3c5308597e02255.tar.gz perlweeklychallenge-club-35a38c808be437c6404f0a02c3c5308597e02255.tar.bz2 perlweeklychallenge-club-35a38c808be437c6404f0a02c3c5308597e02255.zip | |
Merge pull request #12163 from mahnkong/challenge-325
Challenge 325
Diffstat (limited to 'challenge-325')
| -rw-r--r-- | challenge-325/mahnkong/perl/ch-1.pl | 21 | ||||
| -rw-r--r-- | challenge-325/mahnkong/perl/ch-2.pl | 25 |
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"); |
