aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-05-23 23:09:09 +0100
committerGitHub <noreply@github.com>2025-05-23 23:09:09 +0100
commit5ad0dded1d2b168321bb98dbecd272cf819c5f3e (patch)
treea66516d31717cb04a75b062e750cce1e21e34171
parent7292de134b59d6341c80a3e837eeca16d7628873 (diff)
parent1364cc899a4c6792fc8c037933d858fef2cc4b83 (diff)
downloadperlweeklychallenge-club-5ad0dded1d2b168321bb98dbecd272cf819c5f3e.tar.gz
perlweeklychallenge-club-5ad0dded1d2b168321bb98dbecd272cf819c5f3e.tar.bz2
perlweeklychallenge-club-5ad0dded1d2b168321bb98dbecd272cf819c5f3e.zip
Merge pull request #12063 from 0rir/work
322
-rw-r--r--challenge-322/0rir/raku/ch-1.raku53
-rw-r--r--challenge-322/0rir/raku/ch-2.raku56
2 files changed, 109 insertions, 0 deletions
diff --git a/challenge-322/0rir/raku/ch-1.raku b/challenge-322/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..1a0d2a2eb2
--- /dev/null
+++ b/challenge-322/0rir/raku/ch-1.raku
@@ -0,0 +1,53 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴
+use v6.d;
+use Test;
+
+=begin comment
+322-Task 1: String Format Submitted by: Mohammad Sajid Anwar
+
+You are given a string and a positive integer.
+
+Write a script to format the string, removing any dashes, in groups of size given by the integer. The first group can be smaller than the integer but should have at least one character. Groups should be separated by dashes.
+
+Example 1
+Input: $str = "ABC-D-E-F", $i = 3
+Output: "ABC-DEF"
+
+Example 2
+Input: $str = "A-BC-D-E", $i = 2
+Output: "A-BC-DE"
+
+Example 3
+Input: $str = "-A-B-CD-E", $i = 4
+Output: "A-BCDE"
+
+=end comment
+
+my @Test =
+ "ABC-D-E-F", 3, "ABC-DEF",
+ "A-BC-D-E", 2, "A-BC-DE",
+ "-A-B-CD-E", 4, "A-BCDE",
+ 'A-B', 1, 'A-B',
+ 'A-B', 2, 'AB',
+ 'A-B', 3, 'AB',
+ 'A', 5, 'A',
+;
+plan @Test ÷ 3;
+
+sub task( Any:D(Str) $s where * ne '', Any:D(Int) $i, :$sep = '-' -->Str) {
+ $_ = $s.subst( $sep, '', :g).flip;
+ .comb( $i, :partial).join($sep).flip;
+}
+
+for @Test -> $in, $siz, $exp,{
+ is task( $in, $siz), $exp, "{$exp // $exp.^name()} <- $in ∘∘ $siz";
+}
+done-testing;
+
+
+my $str = "-A-B-CD-E";
+my $i = 4;
+
+say qq{\nInput: \$str = "$str" \$i = $i\nOutput: "}, &task($str, $i), '"';
+
diff --git a/challenge-322/0rir/raku/ch-2.raku b/challenge-322/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..9f5ded6485
--- /dev/null
+++ b/challenge-322/0rir/raku/ch-2.raku
@@ -0,0 +1,56 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴
+use v6.e.PREVIEW;
+use Test;
+
+die 'Need rakudo version 2023.08 or later.'
+ unless $*RAKU.compiler.version ≥ v2023.08;
+
+
+=begin comment
+322-2: Rank Array Submitted by: Mohammad Sajid Anwar
+You are given an array of integers.
+
+Write a script to return an array of the ranks of each element: the lowest value has rank 1, next lowest rank 2, etc. If two elements are the same then they share the same rank.
+
+
+Example 1
+Input: @ints = (55, 22, 44, 33)
+Output: (4, 1, 3, 2)
+
+Example 2
+Input: @ints = (10, 10, 10)
+Output: (1, 1, 1)
+
+Example 3
+Input: @ints = (5, 1, 1, 4, 3)
+Output: (4, 1, 1, 3, 2)
+
+=end comment
+
+my @Test =
+ (55, 22, 44, 33), (4, 1, 3, 2),
+ (10, 10, 10), (1, 1, 1),
+ (5, 1, 1, 4, 3), (4, 1, 1, 3, 2),
+;
+
+plan @Test ÷ 2;
+
+sub task( @in -->List) {
+ my @w = @in.Array; # List to Array
+ my %m = @w.sort.squish.antipairs.Array; # map value to rank
+ for 0..^@w -> \i { # use the map
+ @w[i] = %m{@w[i]}+1;
+ }
+ @w.List;
+}
+
+for @Test -> @in, @exp {
+ is-deeply task( @in), @exp, "@exp[] <- @in[]";
+}
+done-testing;
+
+my @int = (5, 1, 1, 4, 3, 1, 1, 3, 5);
+
+say qq{\nInput: @int = @int[]\nOutput: &task(@int)};
+