aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-261/0rir/raku/ch-1.raku81
-rw-r--r--challenge-261/0rir/raku/ch-2.raku72
2 files changed, 153 insertions, 0 deletions
diff --git a/challenge-261/0rir/raku/ch-1.raku b/challenge-261/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..8aa4beec68
--- /dev/null
+++ b/challenge-261/0rir/raku/ch-1.raku
@@ -0,0 +1,81 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6;
+use Test;
+
+=begin comment
+Task 1: Element Digit Sum
+Submitted by: Mohammad Sajid Anwar
+You are given an array of integers, @ints.
+
+Write a script to evaluate the absolute difference between element and digit sum of the given array.
+
+Example 1
+Input: @ints = (1,2,3,45)
+Output: 36
+
+Element Sum: 1 + 2 + 3 + 45 = 51
+Digit Sum: 1 + 2 + 3 + 4 + 5 = 15
+Absolute Difference: | 51 - 15 | = 36
+Example 2
+Input: @ints = (1,12,3)
+Output: 9
+
+Element Sum: 1 + 12 + 3 = 16
+Digit Sum: 1 + 1 + 2 + 3 = 7
+Absolute Difference: | 16 - 7 | = 9
+Example 3
+Input: @ints = (1,2,3,4)
+Output: 0
+
+Element Sum: 1 + 2 + 3 + 4 = 10
+Digit Sum: 1 + 2 + 3 + 4 = 10
+Absolute Difference: | 10 - 10 | = 0
+Example 4
+Input: @ints = (236, 416, 336, 350)
+Output: 1296
+=end comment
+
+=begin comment
+Digit is used variously. Constructs like "single digit number" suggest digit
+is a symbol to construct numbers but not a number itself.
+=end comment
+
+my @Test =
+ 0, 0, (1,2,3,4),
+ 0, 0, (0,1,2,3,4),
+ 9, 9, (1,12,3),
+ 36, 36, (1,2,3,45),
+ 1296, 1296, (236, 416, 336, 350),
+ 9, 9, (1,2,3,15),
+ -33, -9, (-1,-2,-3,-15),
+ -21, -9, (1,2,3,-15),
+ -21, -9, (0,1,2,3,-15),
+ 624, 648, (236, 416, -336, 350),
+;
+
+plan @Test ÷ 3 × 2;
+
+# alway consider "digits" to be positive
+multi digit-sum( $a, Bool:D :$unsigned! -->Int) {
+ $a.sum - sum $a.map: { sum .abs.Str.comb».Int };
+}
+# consider "digits" to inherit the sign of the containing number
+multi digit-sum( $a -->Int) {
+ $a.sum
+ - sum $a.map({
+ if $_ > 0 { sum .Str.comb».Int; }
+ else { - sum .abs.Str.comb».Int; }
+ })
+}
+
+for @Test -> $pos, $signed, $in {
+ #is digit-sum($in, :unsigned), $pos, "$pos\t<- $in :unsigned digits";
+ is digit-sum($in ), $signed, "$signed\t<- $in";
+}
+
+done-testing;
+my $ints = (1, 2, 3, -15);
+say "\nInput: \$ints = $ints.raku()\nOutput: &digit-sum($ints, :unsigned)";
+exit;
+
diff --git a/challenge-261/0rir/raku/ch-2.raku b/challenge-261/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..3503349901
--- /dev/null
+++ b/challenge-261/0rir/raku/ch-2.raku
@@ -0,0 +1,72 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6;
+use Test;
+
+=begin comment
+261-2: Multiply by Two Submitted by: Mohammad Sajid Anwar
+You are given an array of integers, @ints and an integer $start.
+Write a script to do the followings:
+
+a) Look for $start in the array @ints, if found multiply the number by 2
+b) If not found stop the process otherwise repeat
+In the end return the final value.
+
+Example 1
+Input: @ints = (5,3,6,1,12) and $start = 3
+Output: 24
+
+Step 1: 3 is in the array so 3 x 2 = 6
+Step 2: 6 is in the array so 6 x 2 = 12
+Step 3: 12 is in the array so 12 x 2 = 24
+
+24 is not found in the array so return 24.
+Example 2
+Input: @ints = (1,2,4,3) and $start = 1
+Output: 8
+
+Step 1: 1 is in the array so 1 x 2 = 2
+Step 2: 2 is in the array so 2 x 2 = 4
+Step 3: 4 is in the array so 4 x 2 = 8
+
+8 is not found in the array so return 8.
+Example 3
+Input: @ints = (5,6,7) and $start = 2
+Output: 2
+
+2 is not found in the array so return 2.
+
+=end comment
+
+my @Test =
+ # list find exp
+ (), 3, Int,
+ (1,), 0, 0,
+ (1,), 3, 3,
+ (5,3,6,1,12), 3, 24,
+ (5,6,7), 2, 2,
+ (1,2,4,3), 1, 8,
+;
+plan @Test ÷ 3;
+
+multi func( List $list where * ~~ Empty, Any:D $n ) { Int }
+multi func( List $list, $n where * == 0) { 0 }
+multi func( List $list, Any:D $n is copy) {
+ return $n if not $n == $list.any;
+ while so $n == $list.any {
+ $n ×= 2;
+ }
+ return $n;
+}
+
+for @Test -> $list, $n, $exp {
+ is func($list, $n), $exp, ($exp // $exp.^name) ~ " <- $n <- $list";
+}
+
+done-testing;
+my $ints = (2,4,24,48,3,5,6,7,5,3,95,6,12,96);
+my $start = 3;
+say "\nInput: \$ints = $ints.raku() and \$start = $start
+Output: &func($ints, $start)";
+
+