diff options
| -rw-r--r-- | challenge-323/ash/raku/ch-1.raku | 17 | ||||
| -rw-r--r-- | challenge-323/ash/raku/ch-2.raku | 37 |
2 files changed, 54 insertions, 0 deletions
diff --git a/challenge-323/ash/raku/ch-1.raku b/challenge-323/ash/raku/ch-1.raku new file mode 100644 index 0000000000..20b0ec664d --- /dev/null +++ b/challenge-323/ash/raku/ch-1.raku @@ -0,0 +1,17 @@ +# Task 1 of the Weekly Challenge 323 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-323 + +say compute '--x', 'x++', 'x++'; # 1 +say compute 'x++', '++x', 'x++'; # 3 +say compute 'x++', '++x', '--x', 'x--'; # 0 + +sub compute(*@expr) { + my $x = 0; + + for @expr { + when /'++'/ {$x++} + when /'--'/ {$x--} + } + + return $x; +} diff --git a/challenge-323/ash/raku/ch-2.raku b/challenge-323/ash/raku/ch-2.raku new file mode 100644 index 0000000000..91727ca2c9 --- /dev/null +++ b/challenge-323/ash/raku/ch-2.raku @@ -0,0 +1,37 @@ +# Task 2 of the Weekly Challenge 323 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-323 + +say income-tax 10, [[3, 50], [7, 10], [12, 25]]; # 2.65 +say income-tax 2, [[1, 0], [4, 25], [5, 50]]; # 0.25 +say income-tax 0, [[2, 50],]; # 0 + +sub income-tax($income, @levels) { + return 0 unless $income; + + my $remainder = $income; + my $tax = 0; + + + my $prev_threshold = 0; + for @levels -> ($threshold, $rate) { + my $delta = $threshold - $prev_threshold; + + my $taxable = 0; + if $remainder > $delta { + $remainder -= $delta; + $taxable = $delta; + } + else { + $taxable = $remainder; + $remainder = 0; + } + + last unless $taxable; + + $tax += $taxable * $rate / 100; + + $prev_threshold = $threshold; + } + + return $tax; +} |
