From 42f0c43c062be4e2fd3fa1e9ff956a99fd204a07 Mon Sep 17 00:00:00 2001 From: Andrew Shitov Date: Mon, 26 May 2025 10:51:13 +0200 Subject: Week 323 Raku by @ash --- challenge-323/ash/raku/ch-1.raku | 17 +++++++++++++++++ challenge-323/ash/raku/ch-2.raku | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 challenge-323/ash/raku/ch-1.raku create mode 100644 challenge-323/ash/raku/ch-2.raku 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..b243d75668 --- /dev/null +++ b/challenge-323/ash/raku/ch-2.raku @@ -0,0 +1,35 @@ +# 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; + } + + $tax += $taxable * $rate / 100; + + $prev_threshold = $threshold; + } + + return $tax; +} -- cgit From 3fe2807cf99865d74bf0fe2a3488c84e7c67c72a Mon Sep 17 00:00:00 2001 From: Andrew Shitov Date: Mon, 26 May 2025 10:54:17 +0200 Subject: , --- challenge-323/ash/raku/ch-2.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-323/ash/raku/ch-2.raku b/challenge-323/ash/raku/ch-2.raku index b243d75668..aac6590902 100644 --- a/challenge-323/ash/raku/ch-2.raku +++ b/challenge-323/ash/raku/ch-2.raku @@ -3,7 +3,7 @@ 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 +say income-tax 0, [[2, 50],]; # 0 sub income-tax($income, @levels) { return 0 unless $income; -- cgit From 16a99f5bf5681b0adc157f4588672cc7a9ec4da6 Mon Sep 17 00:00:00 2001 From: Andrew Shitov Date: Mon, 26 May 2025 10:56:07 +0200 Subject: truncate the rest of calculations --- challenge-323/ash/raku/ch-2.raku | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/challenge-323/ash/raku/ch-2.raku b/challenge-323/ash/raku/ch-2.raku index aac6590902..91727ca2c9 100644 --- a/challenge-323/ash/raku/ch-2.raku +++ b/challenge-323/ash/raku/ch-2.raku @@ -24,7 +24,9 @@ sub income-tax($income, @levels) { else { $taxable = $remainder; $remainder = 0; - } + } + + last unless $taxable; $tax += $taxable * $rate / 100; -- cgit