diff options
| author | KjetilS <kjetilskotheim@gmail.com> | 2025-05-27 00:29:22 +0200 |
|---|---|---|
| committer | KjetilS <kjetilskotheim@gmail.com> | 2025-05-27 00:29:22 +0200 |
| commit | 33fd0e68ee3b303b1b0cd2e26239d0c299dd1bd2 (patch) | |
| tree | 1737423287735eca62fd8a68a8f0fb2967980306 | |
| parent | 0729d1308bfe2e7d4fc1ea6f41b40356645d4f72 (diff) | |
| download | perlweeklychallenge-club-33fd0e68ee3b303b1b0cd2e26239d0c299dd1bd2.tar.gz perlweeklychallenge-club-33fd0e68ee3b303b1b0cd2e26239d0c299dd1bd2.tar.bz2 perlweeklychallenge-club-33fd0e68ee3b303b1b0cd2e26239d0c299dd1bd2.zip | |
https://theweeklychallenge.org/blog/perl-weekly-challenge-323/
| -rw-r--r-- | challenge-323/kjetillll/perl/ch-1.pl | 4 | ||||
| -rw-r--r-- | challenge-323/kjetillll/perl/ch-2.pl | 40 |
2 files changed, 44 insertions, 0 deletions
diff --git a/challenge-323/kjetillll/perl/ch-1.pl b/challenge-323/kjetillll/perl/ch-1.pl new file mode 100644 index 0000000000..5b2b2ffe4a --- /dev/null +++ b/challenge-323/kjetillll/perl/ch-1.pl @@ -0,0 +1,4 @@ +sub f { "@_" =~ y/+// - @_ } +print f("--x", "x++", "x++") == 1 ? "😊\n" : "💀\n"; +print f("x++", "++x", "x++") == 3 ? "😊\n" : "💀\n"; +print f("x++", "++x", "--x", "x--") == 0 ? "😊\n" : "💀\n"; diff --git a/challenge-323/kjetillll/perl/ch-2.pl b/challenge-323/kjetillll/perl/ch-2.pl new file mode 100644 index 0000000000..27a405a096 --- /dev/null +++ b/challenge-323/kjetillll/perl/ch-2.pl @@ -0,0 +1,40 @@ +sub f_alternative_1 { + my($in, @b) = @_; + my $tax = 0; + my $in_taxed = 0; + while($in > 0 and @b){ + my($upto, $percent) = @{ shift @b }; + $upto -= $in_taxed; + my $amount = $upto < $in ? $upto : $in; + $tax += $amount * $percent / 100; + $in -= $amount; + $in_taxed += $amount; + } + $tax +} + +sub f_alternative_2 { + my($income,@brackets)=@_; + !ref$income ? f( [$income,0], @brackets ) + :!@brackets ? 0 + :$$income[0] <= 0 ? 0 + :do{ + my($income_left, $income_taxed_so_far) = @$income; + my($upto, $percent) = @{ shift @brackets }; + $upto -= $income_taxed_so_far; + my $amount = $income_left > $upto ? $upto : $income_left; + $amount * $percent / 100 + f( [$income_left - $amount, $income_taxed_so_far + $amount], @brackets ) + } +} + +sub f_alternative_3 { + my $income = shift; + my @percent; @percent[0 .. $$_[0]-1] = ( $$_[1] / 100) x $$_[0] for reverse @_; + sprintf "%f", eval join '+', map shift@percent, 1 .. $income +} + +*f = *f_alternative_3; + +print f( 10, [3, 50], [7, 10], [12,25] ) == 2.65 ? "😊\n" : "💀\n"; +print f( 2, [1, 0], [4, 25], [5,50] ) == 0.25 ? "😊\n" : "💀\n"; +print f( 0, [2, 50] ) == 0 ? "😊\n" : "💀\n"; |
