aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-05-30 23:42:00 +0100
committerGitHub <noreply@github.com>2025-05-30 23:42:00 +0100
commit48bc45bb8c0d7ad275d330e1798e9e0880915cc3 (patch)
tree332c2050329968745b5058c6e5cd43a715983f7e
parent70e2f3320050bf193bf74cf1a05b65fe62590004 (diff)
parent49e671ed05a81a931d259044d99281751a9c4f71 (diff)
downloadperlweeklychallenge-club-48bc45bb8c0d7ad275d330e1798e9e0880915cc3.tar.gz
perlweeklychallenge-club-48bc45bb8c0d7ad275d330e1798e9e0880915cc3.tar.bz2
perlweeklychallenge-club-48bc45bb8c0d7ad275d330e1798e9e0880915cc3.zip
Merge pull request #12106 from 0rir/work
223
-rw-r--r--challenge-323/0rir/raku/ch-1.raku63
-rw-r--r--challenge-323/0rir/raku/ch-2.raku95
2 files changed, 158 insertions, 0 deletions
diff --git a/challenge-323/0rir/raku/ch-1.raku b/challenge-323/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..1f72c5573c
--- /dev/null
+++ b/challenge-323/0rir/raku/ch-1.raku
@@ -0,0 +1,63 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴
+use v6.d;
+use Test;
+
+=begin comment
+323-Task 1: Increment Decrement Submitted by: Mohammad Sajid Anwar
+You are given a list of operations.
+Write a script to return the final value after performing the given operations in order. The initial value is always 0.
+
+Possible Operations:
+++x or x++: increment by 1
+--x or x--: decrement by 1
+
+Example 1
+Input: @operations = ("--x", "x++", "x++")
+Output: 1
+
+Operation "--x" => 0 - 1 => -1
+Operation "x++" => -1 + 1 => 0
+Operation "x++" => 0 + 1 => 1
+
+Example 2
+Input: @operations = ("x++", "++x", "x++")
+Output: 3
+
+Example 3
+Input: @operations = ("x++", "++x", "--x", "x--")
+Output: 0
+
+Operation "x++" => 0 + 1 => 1
+Operation "++x" => 1 + 1 => 2
+Operation "--x" => 2 - 1 => 1
+Operation "x--" => 1 - 1 => 0
+=end comment
+
+my @Test =
+ # op-strings exp
+ ("x++",), 1,
+ ("--x",), -1,
+ ("--x", "x++", "x++"), 1,
+ ("x++", "++x", "x++"), 3,
+ ("x++", "++x", "--x", "x--"), 0,
+;
+plan +@Test;
+
+# trustfully
+sub task( @expr, $seed = 0 -->Int) {
+ $seed + @expr - 2 × @expr.grep( *.contains: '-');
+}
+
+for @Test -> @in, $exp is rw {
+ is task( @in), $exp, "{$exp // $exp.^name()} <- @in.raku()";
+ my $seed = 10;
+ my $ex = $exp + $seed;
+ is task( @in, $seed), $ex, "{$ex // $ex.^name()} <- @in.raku() ∘∘ $seed";
+}
+done-testing;
+
+my @op = ("x++", "++x", "--x", "x--");
+
+say qq{\nInput: @operations = @op.raku()\nOutput: }, task @op;
+
diff --git a/challenge-323/0rir/raku/ch-2.raku b/challenge-323/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..1c7b7b7636
--- /dev/null
+++ b/challenge-323/0rir/raku/ch-2.raku
@@ -0,0 +1,95 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴
+use v6.d;
+use Test;
+
+=begin comment
+323-2: Tax Amount Submitted by: Mohammad Sajid Anwar
+You are given an income amount and tax brackets.
+Write a script to calculate the total tax amount.
+
+Example 1
+Input: $income = 10, @tax = ([3, 50], [7, 10], [12,25])
+Output: 2.65
+
+1st tax bracket upto 3, tax is 50%.
+2nd tax bracket upto 7, tax is 10%.
+3rd tax bracket upto 12, tax is 25%.
+
+Total Tax => (3 * 50/100) + (4 * 10/100) + (3 * 25/100)
+ => 1.50 + 0.40 + 0.75
+ => 2.65
+
+Example 2
+Input: $income = 2, @tax = ([1, 0], [4, 25], [5,50])
+Output: 0.25
+
+Total Tax => (1 * 0/100) + (1 * 25/100)
+ => 0 + 0.25
+ => 0.25
+
+Example 3
+Input: $income = 0, @tax = ([2, 50])
+Output: 0
+=end comment
+
+my @Test =
+ # income tax-sched exp
+ # [bracket-end, tax-rate]
+ [ 10, ( [3,50], [7,10], [12,25],), 2.65, ],
+ [ 2, ( [1, 0], [4 , 25], [5 , 50],), 0.25, ],
+ [ 0, ([2, 50],), 0, ],
+;
+plan +@Test;
+
+# characteristics of a tax bracket
+enum Bracket< LOWER UPPER RATE TX-BASE TX-IN TX-MAX>;
+
+# Cache and stack the brackets so only one bracket need be in tax calculation.
+sub create-brackets( @sch -->Tax-schedule) {
+ my $idx = ^@sch;
+ my @ret = @sch.Array.deepmap({$_});
+
+ for @$idx -> \i {
+ @ret[i][RATE] = @sch[i][1] ÷ 100;
+ @ret[i][UPPER] = @sch[i][0];
+
+ @ret[i][LOWER] = i == $idx.min ?? 0 !! @ret[i-1][UPPER];
+ # tax for whole span of bracket
+ @ret[i][TX-IN] = ( @ret[i][UPPER] - @ret[i][LOWER]) × @ret[i][RATE];
+ # tax for all lower brackets
+ @ret[i][TX-BASE] = i == $idx.min ?? 0
+ !! @ret[i-1][TX-BASE] +@ret[i-1][TX-IN];
+ # total tax at top of bracket
+ @ret[i][TX-MAX] = i == $idx.min ?? @ret[i][TX-IN]
+ !! @ret[i-1][TX-MAX] + @ret[i][TX-IN];
+ }
+ if @ret[*-1][UPPER] < ∞ {
+ # create a bracket for income above highest given bracket
+ @ret.push: [ @ret[*-1][UPPER], ∞, 0, @ret[*-1][TX-MAX]];
+ }
+ # delete unneeded elements
+ @ret.=map: {.[LOWER,UPPER,RATE,TX-BASE] };
+}
+
+sub task( $income, @tax-sched -->Rat) {
+ # make applicable tax schedule
+ my @sch= create-brackets( @tax-sched);
+ my $i = 0;
+ ++$i while $income > @sch[$i][UPPER];
+ @sch[$i][TX-BASE] + ($income - @sch[$i][LOWER]) × @sch[$i][RATE]
+}
+
+for @Test -> @data {
+ for @data -> $income, @sched, $exp {
+ is task($income, @sched), $exp,
+ "{$exp // $exp.^name()} <- $income ∘∘ @sched.raku()";
+ }
+}
+done-testing;
+
+my $income = 10;
+my @tax = ([3, 50], [7, 10], [12,25]);
+
+say qq{\nInput: \$income = $income, @tax = @tax.raku()\nOutput: }, task($income, @tax);
+say qq{@tax = @tax.raku()};