From fff6173275bbbcea87577e59b316889bc3ed2660 Mon Sep 17 00:00:00 2001 From: rir Date: Thu, 22 May 2025 12:39:03 -0400 Subject: 322 --- challenge-322/0rir/raku/ch-1.raku | 53 ++++++++++++++++++++++++++++++++++++ challenge-322/0rir/raku/ch-2.raku | 56 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 challenge-322/0rir/raku/ch-1.raku create mode 100644 challenge-322/0rir/raku/ch-2.raku 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)}; + -- cgit