diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-05-27 11:17:34 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-27 11:17:34 +0100 |
| commit | 196de4df18793aa382f31f898bd68308acafba01 (patch) | |
| tree | 537a6ce8143c5c026e6dca40ecda25c9776e1043 | |
| parent | e916849bbec3ef37ca25526ab6118a475ac16da1 (diff) | |
| parent | 1d5a3642a37f9305eec2450bc93af22d82de6c82 (diff) | |
| download | perlweeklychallenge-club-196de4df18793aa382f31f898bd68308acafba01.tar.gz perlweeklychallenge-club-196de4df18793aa382f31f898bd68308acafba01.tar.bz2 perlweeklychallenge-club-196de4df18793aa382f31f898bd68308acafba01.zip | |
Merge pull request #12085 from mahnkong/challenge-323
Challenge 323
| -rw-r--r-- | challenge-323/mahnkong/perl/ch-1.pl | 21 | ||||
| -rw-r--r-- | challenge-323/mahnkong/perl/ch-2.pl | 18 |
2 files changed, 39 insertions, 0 deletions
diff --git a/challenge-323/mahnkong/perl/ch-1.pl b/challenge-323/mahnkong/perl/ch-1.pl new file mode 100644 index 0000000000..614b875061 --- /dev/null +++ b/challenge-323/mahnkong/perl/ch-1.pl @@ -0,0 +1,21 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub inc($number) { return $number += 1; } +sub dec($number) { return $number -= 1; } + +my %ops_map = ('x++' => \&inc, '++x' => \&inc, 'x--' => \&dec, '--x' => \&dec); + +sub run(@operations) { + my $result = 0; + foreach my $operation (@operations) { + $result = $ops_map{$operation}->($result) if exists $ops_map{$operation}; + } + return $result; +} + +is(run("--x", "x++", "x++"), 1, "Example 1"); +is(run("x++", "++x", "x++"), 3, "Example 2"); +is(run("x++", "++x", "--x", "x--"), 0, "Example 3"); diff --git a/challenge-323/mahnkong/perl/ch-2.pl b/challenge-323/mahnkong/perl/ch-2.pl new file mode 100644 index 0000000000..b75665f412 --- /dev/null +++ b/challenge-323/mahnkong/perl/ch-2.pl @@ -0,0 +1,18 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub run($income, @tax) { + my $result = 0; + + for (my $i = 0; $i <= $#tax; $i++) { + my $income_part = ($income >= $tax[$i]->[0] ? $tax[$i]->[0] : $income) - ($i > 0 ? $tax[$i-1]->[0] : 0); + $result += $income_part * ($tax[$i]->[1] / 100) if $income_part >= 0; + } + return $result; +} + +is(run(10, ([3, 50], [7, 10], [12, 25])), 2.65, "Example 1"); +is(run(2, ([1, 0], [4, 25], [5, 50])), 0.25, "Example 2"); +is(run(0, ([2, 50])), 0, "Example 3"); |
