aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-323/e-choroba/perl/ch-1.pl16
-rwxr-xr-xchallenge-323/e-choroba/perl/ch-2.pl29
2 files changed, 45 insertions, 0 deletions
diff --git a/challenge-323/e-choroba/perl/ch-1.pl b/challenge-323/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..ba7061170b
--- /dev/null
+++ b/challenge-323/e-choroba/perl/ch-1.pl
@@ -0,0 +1,16 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub increment_decrement(@operations) {
+ return @operations - 2 * grep /--/, @operations
+}
+
+use Test::More tests => 3 + 1;
+
+is increment_decrement('--x', 'x++', 'x++'), 1, 'Example 1';
+is increment_decrement('x++', '++x', 'x++'), 3, 'Example 2';
+is increment_decrement('x++', '++x', '--x', 'x--'), 0, 'Example 3';
+
+is increment_decrement(), 0, 'Empty';
diff --git a/challenge-323/e-choroba/perl/ch-2.pl b/challenge-323/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..f7fd879a3a
--- /dev/null
+++ b/challenge-323/e-choroba/perl/ch-2.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub tax_amount($income, @tax) {
+ @tax = sort { $a->[0] <=> $b->[0] } @tax;
+ my $from = 0;
+ my $tax_total = 0;
+ while (@tax && $from <= $income) {
+ my ($bracket, $percent) = @{ shift @tax };
+ my $to = $bracket < $income ? $bracket : $income;
+ my $part = $to - $from;
+ $tax_total += $part * $percent / 100;
+ $from = $bracket;
+ }
+ return $tax_total
+}
+
+use Test2::V0;
+plan(3 + 3);
+
+is tax_amount(10, [3, 50], [7, 10], [12, 25]), 2.65, 'Example 1';
+is tax_amount(2, [1, 0], [4, 25], [5, 50]), 0.25, 'Example 2';
+is tax_amount(0, [2, 50]), 0, 'Example 3';
+
+is tax_amount(10, [7, 10], [12,25], [3, 50], ), 2.65, 'Unsorted';
+is tax_amount(10, [0, 100]), 0, 'Bracket 0';
+is tax_amount(100, [10, 10], [50, 20]), 9, 'Largest bracket < income';