From 1d5a3642a37f9305eec2450bc93af22d82de6c82 Mon Sep 17 00:00:00 2001 From: Andreas Mahnke Date: Mon, 26 May 2025 11:56:23 +0200 Subject: Challenge 323 --- challenge-323/mahnkong/perl/ch-1.pl | 21 +++++++++++++++++++++ challenge-323/mahnkong/perl/ch-2.pl | 18 ++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 challenge-323/mahnkong/perl/ch-1.pl create mode 100644 challenge-323/mahnkong/perl/ch-2.pl 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"); -- cgit