diff options
| author | E. Choroba <choroba@matfyz.cz> | 2025-05-26 11:30:09 +0200 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2025-05-26 11:30:09 +0200 |
| commit | 26e9be2e6bcaaa41598d8852f97827efdfb418f0 (patch) | |
| tree | d0e3e9c2f6a979f0045d12bed486ce08621337a8 | |
| parent | 0729d1308bfe2e7d4fc1ea6f41b40356645d4f72 (diff) | |
| download | perlweeklychallenge-club-26e9be2e6bcaaa41598d8852f97827efdfb418f0.tar.gz perlweeklychallenge-club-26e9be2e6bcaaa41598d8852f97827efdfb418f0.tar.bz2 perlweeklychallenge-club-26e9be2e6bcaaa41598d8852f97827efdfb418f0.zip | |
Add solutions to 323: Increment Decrement & Tax Amount by E. Choroba
| -rwxr-xr-x | challenge-323/e-choroba/perl/ch-1.pl | 16 | ||||
| -rwxr-xr-x | challenge-323/e-choroba/perl/ch-2.pl | 29 |
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'; |
