aboutsummaryrefslogtreecommitdiff
path: root/challenge-321
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-321')
-rw-r--r--challenge-321/0rir/raku/ch-1.raku60
-rw-r--r--challenge-321/0rir/raku/ch-2.raku106
2 files changed, 166 insertions, 0 deletions
diff --git a/challenge-321/0rir/raku/ch-1.raku b/challenge-321/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..f7a84bd09d
--- /dev/null
+++ b/challenge-321/0rir/raku/ch-1.raku
@@ -0,0 +1,60 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴
+use v6.d;
+use Test;
+
+=begin comment
+Task 1: Distinct Average Submitted by: Mohammad Sajid Anwar
+ [edited]
+You are given an array of numbers with even length.
+
+Write a script to return the count of distinct average. The average is
+calculated, by repeatedly, removing the minimum and the maximum, then average of the two. See notes to examples.
+
+Example 1
+Input: @nums = (1, 2, 4, 3, 5, 6)
+Output: 1
+
+Step 1: Min = 1, Max = 6, Avg = 3.5
+Step 2: Min = 2, Max = 5, Avg = 3.5
+Step 3: Min = 3, Max = 4, Avg = 3.5
+
+The count of distinct average is 1.
+
+Example 2
+Input: @nums = (0, 2, 4, 8, 3, 5)
+Output: 2
+
+Example 3
+Input: @nums = (7, 3, 1, 0, 5, 9)
+Output: 2
+
+=end comment
+
+my @Test =
+ # in exp
+ (1, 2, 4, 3, 5, 6), 1,
+ (0, 2, 4, 8, 3, 5), 2,
+ (7, 3, 1, 0, 5, 9), 2,
+ (1,1), 1,
+ (2,1), 1,
+;
+plan @Test ÷ 2;
+
+sub task( $a is copy -->Int) {
+ $a = $a.sort;
+ my \i = $a.end div 2;
+ ( [Z+] $a[0..i],
+ $a[$a.end … i+1]
+ ).unique.elems;
+}
+
+for @Test -> @in, $exp, {
+ is task( @in), $exp, "{$exp // $exp.^name()} <- @in.raku()";
+}
+done-testing;
+
+my @num = (7, 3, 1, 0, 5, 9);
+
+say "\nInput: @nums = @num.raku()\nOutput: &task( @num)";
+
diff --git a/challenge-321/0rir/raku/ch-2.raku b/challenge-321/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..9d0e8856dc
--- /dev/null
+++ b/challenge-321/0rir/raku/ch-2.raku
@@ -0,0 +1,106 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴
+use v6.d;
+use Test;
+
+=begin comment
+321-Task 2: Backspace Compare Submitted by: Mohammad Sajid Anwar
+You are given two strings containing zero or more #.
+
+Write a script to return true if the two given strings are same by treating # as backspace.
+
+
+Example 1
+Input: $str1 = "ab#c"
+ $str2 = "ad#c"
+Output: true
+
+For first string, we remove "b" as it is followed by "#".
+For second string, we remove "d" as it is followed by "#".
+In the end both strings became the same.
+
+Example 2
+Input: $str1 = "ab##"
+ $str2 = "a#b#"
+Output: true
+
+Example 3
+Input: $str1 = "a#b"
+ $str2 = "c"
+Output: false
+=end comment
+
+my @Test =
+ "ab*c", "ad*c", True,
+ "ab**", "a*b*", True,
+ "***ab**", "**a*b*", True,
+ "ab**", "***a*b*", True,
+
+ "a*b", "c", False,
+ "*a*b", "c", False,
+ "*a*b", "***c", False,
+;
+
+my @Test-BS =
+ "***c", "c",
+ "ab*c", "ac",
+ "ab**", "",
+ "a*b*", "",
+ "***ab**", "",
+ "***ab****", "",
+ "*a*b*", "",
+ "ab**", "",
+ "ab****", "",
+ "a*b", "b",
+ "c", "c",
+ "*a*b", "b",
+ '*', '',
+;
+plan @Test ÷ 3 + @Test-BS ÷ 2;
+
+my $BS-CHAR = '*'; # Using '*' to avoid Test::proclaim's munging.
+
+# task -- return the 'cmp' of two Strs after processing "backspace"
+# characters. Leading "backspace"s are deleteable.
+sub task( $a is copy, $b is copy, :$bs = $BS-CHAR -->Bool) {
+ my @bs;
+ for $a, $b -> $word {
+ @bs.push: apply-bs( $word);
+ }
+ return @bs[0] eq @bs[1];
+}
+
+# apply-bs
+# Process "backspace" characters in an Array repping a string.
+sub apply-bs( $w, :$bs = $BS-CHAR -->Str) {
+ my @w = $w.comb;
+ my $k = -1;
+ my @ret;
+
+ while $k < @w.end {
+ ++$k;
+ when @w[$k] ne $bs {
+ @ret.push: @w[$k];
+ }
+ when @w[$k] eq $bs and @ret.elems > 0 {
+ @ret.pop;
+ }
+ # nought to do: @w[$k] eq $bs and @ret ~~ Empty
+ }
+ @ret.join;
+}
+for @Test-BS -> $in, $exp {
+ is apply-bs( $in), $exp, "$exp <- " ~ $in.raku();
+}
+
+for @Test -> $a, $b, $exp {
+ is task($a, $b ), $exp, "$exp <- @$a[] ∘∘ @$b[]";
+}
+done-testing;
+
+$BS-CHAR = '#';
+my $str1 = "ab#xy##c#####a#b#abcdefg######ce###acb#";
+my $str2 = "ad#c";
+
+say "\nInput: \$str1 = $str1\n \$str2 = $str2\nOutput: ",
+ task( $str1, $str2);