aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-05-18 16:07:45 +0100
committerGitHub <noreply@github.com>2023-05-18 16:07:45 +0100
commit93ea89f65b70f87194ea110726646901c2772f1d (patch)
tree969fd280dd139ce01f641b3808e39e57a645f4e3
parent3fe4c2b48433cb2d11888fc3a2cfd7bf5dbd846c (diff)
parent4a3c95230c560bc8024643a0758f84ece78b6397 (diff)
downloadperlweeklychallenge-club-93ea89f65b70f87194ea110726646901c2772f1d.tar.gz
perlweeklychallenge-club-93ea89f65b70f87194ea110726646901c2772f1d.tar.bz2
perlweeklychallenge-club-93ea89f65b70f87194ea110726646901c2772f1d.zip
Merge pull request #8100 from 0rir/217
217
-rw-r--r--challenge-217/0rir/raku/ch-1.raku50
-rw-r--r--challenge-217/0rir/raku/ch-2.raku96
2 files changed, 146 insertions, 0 deletions
diff --git a/challenge-217/0rir/raku/ch-1.raku b/challenge-217/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..815e87daef
--- /dev/null
+++ b/challenge-217/0rir/raku/ch-1.raku
@@ -0,0 +1,50 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6.d;
+use Test;
+
+=begin comment
+217-1: Sorted Matrix Submitted by: Mohammad S Anwar
+Given a n x n matrix where n >= 2, 3rd smallest element in the sorted matrix.
+
+Example 1
+Input: @matrix = ([3, 1, 2], [5, 2, 4], [0, 1, 3])
+Output: 1
+
+The sorted list of the given matrix: 0, 1, 1, 2, 2, 3, 3, 4, 5.
+The 3rd smallest of the sorted list is 1.
+Example 2
+Input: @matrix = ([2, 1], [4, 5])
+Output: 4
+
+The sorted list of the given matrix: 1, 2, 4, 5.
+The 3rd smallest of the sorted list is 4.
+Example 3
+Input: @matrix = ([1, 0, 3], [0, 0, 0], [1, 2, 1])
+Output: 0
+
+The sorted list of the given matrix: 0, 0, 0, 0, 1, 1, 1, 2, 3.
+The 3rd smallest of the sorted list is 0.
+=end comment
+
+my @Test =
+ ([3, 1, 2], [5, 2, 4], [0, 1, 3]), ( 0,1,1,2,2,3,3,4,5),
+ ([1, 0, 3], [0, 0, 0], [1, 2, 1]), ( 0,0,0,0,1,1,1,2,3),
+ ([2, 1], [4, 5]) , ( 1,2,4,5),
+;
+
+plan @Test/2;
+
+sub iron-n-sort( @a -->Array) {
+ @a.flat.sort.Array;
+}
+
+for @Test -> @in, @exp {
+ is iron-n-sort(@in), @exp, "@in[] --> @exp[]";
+}
+done-testing;
+
+my $matrix = ([2, 1], [4, 5]);
+say "\nInput: @matrix = ", $matrix, "\nOutput: ", (iron-n-sort $matrix)[2];
+exit;
+
diff --git a/challenge-217/0rir/raku/ch-2.raku b/challenge-217/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..6bcaaa7304
--- /dev/null
+++ b/challenge-217/0rir/raku/ch-2.raku
@@ -0,0 +1,96 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6.d;
+use Test;
+
+=begin comment
+217-2: Max Number Submitted by: Mohammad S Anwar
+
+Given a list of positive integers, concatenate the integers to form the highest possible value.
+
+Example 1:
+Input: @list = (1, 23)
+Output: 231
+Example 2:
+Input: @list = (10, 3, 2)
+Output: 3210
+Example 3:
+Input: @list = (31, 2, 4, 10)
+Output: 431210
+Example 4:
+Input: @list = (5, 11, 4, 1, 2)
+Output: 542111
+Example 5:
+Input: @list = (1, 10)
+Output: 110
+=end comment
+
+my @Test-cmp =
+ (1 , 1 ), Same,
+ (12 , 12 ), Same,
+ (123 , 123), Same,
+ (1231234, 123), More,
+ (123133 , 123), More,
+ (12313 , 123), More,
+ (123111 , 123), Less,
+ (12311 , 123), Less,
+ (1231 , 123), Less,
+ (1 , 23 ), Less,
+ (10 , 3 ), Less,
+ (31 , 2 ), More,
+ (2 , 4 ), Less,
+ (4 , 10 ), More,
+ (5 , 11 ), More,
+ (11 , 4 ), Less,
+ (1 , 10 ), More,
+;
+
+my @Test =
+ (1, 23), 231,
+ (10, 3, 2), 3210,
+ (31, 2, 4, 10), 431210,
+ (5, 11, 4, 1, 2), 542111,
+ (1, 10), 110,
+;
+
+plan @Test-cmp + @Test/2;
+
+sub cat-cmp( Any:D $a, Any:D $b ) {
+ my @a = $a.comb.Array;
+ my @b = $b.comb.Array;
+ my $span = (@a.elems, @b.elems).min;
+ for ^$span -> $i {
+ my $o = @a[$i] <=> @b[$i];
+ if ? $o { return $o }
+ }
+
+ given +@a <=> +@b {
+ when Same { return Same }
+ when More {
+ return cat-cmp @a[$span..*].join, @b.join;
+ }
+ when Less {
+ return cat-cmp @a.join, @b[$span..*].join;
+ }
+ }
+}
+
+sub max-number( @a -->Int) {
+ (@a.sort: { cat-cmp $^b.Int, $^a.Int } ).join.Int;
+}
+
+for @Test-cmp -> @in, $exp is rw {
+ is cat-cmp( @in[0], @in[1]), $exp, "$exp <-- cat-cmp @in[0] @in[1]";
+ $exp = ( $exp × -1).Order;
+ is cat-cmp( @in[1], @in[0]), $exp, "$exp <-- cat-cmp @in[1] @in[0]";
+
+}
+for @Test -> @in, $exp {
+ is max-number(@in), $exp, "$exp <-- @in.raku()";
+}
+done-testing;
+
+my @list = (5, 11, 4, 1, 2);
+say "\nInput: @list = @list[]\nOutput: ", max-number( @list);
+exit;
+