diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-05-27 11:16:53 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-27 11:16:53 +0100 |
| commit | fecbe0bd69945d0cb07528d547154272dcdc8ea7 (patch) | |
| tree | 4ceed6e7700a06968b7d63b242396a14a2c5ae31 | |
| parent | 422d224259e8ce09127b31bff8183dedc9c6ff18 (diff) | |
| parent | 16a99f5bf5681b0adc157f4588672cc7a9ec4da6 (diff) | |
| download | perlweeklychallenge-club-fecbe0bd69945d0cb07528d547154272dcdc8ea7.tar.gz perlweeklychallenge-club-fecbe0bd69945d0cb07528d547154272dcdc8ea7.tar.bz2 perlweeklychallenge-club-fecbe0bd69945d0cb07528d547154272dcdc8ea7.zip | |
Merge pull request #12083 from ash/ash-week-323
Week 323 Raku by @ash
| -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; +} |
