diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-08-26 14:37:20 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-26 14:37:20 +0100 |
| commit | fa09b12662f911afa328e1ae9b0303f5bb4ce9c2 (patch) | |
| tree | 71dddb46c3e8814d3749b8cfe85858468bc9d53f | |
| parent | 68e6272f3ccb677b4648e702c8517ac3a9fec4f1 (diff) | |
| parent | b91875a2550e5b989fb929ed8c20aefb80521eb2 (diff) | |
| download | perlweeklychallenge-club-fa09b12662f911afa328e1ae9b0303f5bb4ce9c2.tar.gz perlweeklychallenge-club-fa09b12662f911afa328e1ae9b0303f5bb4ce9c2.tar.bz2 perlweeklychallenge-club-fa09b12662f911afa328e1ae9b0303f5bb4ce9c2.zip | |
Merge pull request #12582 from mahnkong/challenge-336
Challenge 336
| -rw-r--r-- | challenge-336/mahnkong/perl/ch-1.pl | 29 | ||||
| -rw-r--r-- | challenge-336/mahnkong/perl/ch-2.pl | 30 |
2 files changed, 59 insertions, 0 deletions
diff --git a/challenge-336/mahnkong/perl/ch-1.pl b/challenge-336/mahnkong/perl/ch-1.pl new file mode 100644 index 0000000000..f7195058e4 --- /dev/null +++ b/challenge-336/mahnkong/perl/ch-1.pl @@ -0,0 +1,29 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub run(@ints) { + for (my $i = scalar(@ints)/2; $i >= 2; $i--) { + if (scalar(@ints) % $i == 0) { + my @work = @ints; + + my $valid = 1; + while (scalar(@work)) { + my %values; + foreach my $v (splice(@work, 0, $i)) { + $values{$v} = 1; + } + $valid = 0 if scalar(keys(%values)) != 1; + } + return 1 if $valid; + } + } + return 0; +} + +is(run(1,1,2,2,2,2), 1, "Example 1"); +is(run(1,1,1,2,2,2,3,3), 0, "Example 2"); +is(run(5,5,5,5,5,5,7,7,7,7,7,7), 1, "Example 3"); +is(run(1,2,3,4), 0, "Example 4"); +is(run(8,8,9,9,10,10,11,11), 1, "Example 5"); diff --git a/challenge-336/mahnkong/perl/ch-2.pl b/challenge-336/mahnkong/perl/ch-2.pl new file mode 100644 index 0000000000..8fe49b7d22 --- /dev/null +++ b/challenge-336/mahnkong/perl/ch-2.pl @@ -0,0 +1,30 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub run(@scores) { + my $total = 0; + my @result; + + foreach my $score (@scores) { + if ($score =~ /^[+-]?\d+$/) { + push @result, $score; + } elsif ($score eq 'D') { + push @result, $result[-1]*2; + } elsif ($score eq '+') { + push @result, $result[-2] + $result[-1]; + } elsif ($score eq 'C') { + delete $result[-1]; + } + } + + map { $total += $_ } @result; + return $total; +} + +is(run("5","2","C","D","+"), 30, "Example 1"); +is(run("5","-2","4","C","D","9","+","+"), 27, "Example 2"); +is(run("7","D","D","C","+","3"), 45, "Example 3"); +is(run("-5","-10","+","D","C","+"), -55, "Example 4"); +is(run("3","6","+","D","C","8","+","D","-2","C","+"), '128', "Example 5"); |
