aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-05-13 13:27:12 +0100
committerGitHub <noreply@github.com>2025-05-13 13:27:12 +0100
commitd12ddc5cf0ff45cbb4114dd4570baa8977ba52b3 (patch)
tree1dff140dd7e89ca523ab726038ccd0370c67177e
parent4fa3f69176091cafd39d2fd070c25fcc4d2a11b7 (diff)
parent5beac96bbdbef29898af45fece5d405cb7c3be79 (diff)
downloadperlweeklychallenge-club-d12ddc5cf0ff45cbb4114dd4570baa8977ba52b3.tar.gz
perlweeklychallenge-club-d12ddc5cf0ff45cbb4114dd4570baa8977ba52b3.tar.bz2
perlweeklychallenge-club-d12ddc5cf0ff45cbb4114dd4570baa8977ba52b3.zip
Merge pull request #12022 from 0rir/work
321
-rw-r--r--challenge-320/0rir/raku/ch-1.raku40
-rw-r--r--challenge-320/0rir/raku/ch-2.raku38
-rw-r--r--challenge-321/0rir/raku/ch-1.raku60
-rw-r--r--challenge-321/0rir/raku/ch-2.raku106
4 files changed, 244 insertions, 0 deletions
diff --git a/challenge-320/0rir/raku/ch-1.raku b/challenge-320/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..c8fc36a119
--- /dev/null
+++ b/challenge-320/0rir/raku/ch-1.raku
@@ -0,0 +1,40 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴
+use v6.d;
+use Test;
+
+=begin comment
+320-Task 1: Maximum Count Submitted by: Mohammad Sajid Anwar
+You are given an array of integers.
+
+Write a script to return the maximum between the number of positive and negative integers. Zero is neither positive nor negative.
+
+
+Example 1
+Input: @ints = (-3, -2, -1, 1, 2, 3)
+Output: 3
+
+…
+=end comment
+
+my @Test =
+ # in exp
+ (-3, -2, -1, 1, 2, 3), 3,
+ (-2, -1, 0, 0, 1), 2,
+ (1, 2, 3, 4), 4,
+ (0,0), 0,
+ (9,), 1,
+ (-9,), 1,
+;
+plan @Test ÷ 2;
+
+sub task( @a) { max +@a.grep( * > 0), +@a.grep( * < 0) }
+
+for @Test -> @in, $exp {
+ is task( @in), $exp, "{$exp // $exp.^name()} <- @in.raku()";
+}
+done-testing;
+
+my @int = -3, -2, -1, 1, 2, 3;
+
+say qq{\nInput: @int = @int.raku()\nOutput: }, task @int;
diff --git a/challenge-320/0rir/raku/ch-2.raku b/challenge-320/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..e8d6f180c4
--- /dev/null
+++ b/challenge-320/0rir/raku/ch-2.raku
@@ -0,0 +1,38 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴
+use v6.d;
+use Test;
+
+=begin comment
+320-2: Sum Difference Submitted by: Mohammad Sajid Anwar
+You are given an array of positive integers.
+
+Write a script to return the absolute difference between digit sum and element sum of the given array.
+
+Example 1
+Input: @ints = (1, 23, 4, 5)
+Output: 18
+…
+=end comment
+
+my @Test =
+ # in exp
+ (1, 23, 4, 5), 18,
+ (1, 2, 3, 4, 5), 0,
+ (1, 2, 34), 27,
+ (), 0,
+ (12,), 9,
+;
+
+plan @Test ÷ 2;
+
+sub task( @a) { ( @a.sum - @a.join.comb.sum ).abs }
+
+for @Test -> @in, $exp {
+ is task( @in), $exp, "{$exp // $exp.^name()} <- @in.raku()";
+}
+done-testing;
+
+my @int = 1, 23, 4, 5;
+
+say qq{\nInput: @int = @int.raku()\nOutput: }, task( @int);
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);