aboutsummaryrefslogtreecommitdiff
path: root/challenge-204/0rir
diff options
context:
space:
mode:
authorrir <rirans@comcast.net>2023-02-17 12:27:09 -0500
committerrir <rirans@comcast.net>2023-02-17 12:27:09 -0500
commit9d4724c13ae67bdc3669bf36ee9e75e88f2e1e84 (patch)
tree49e77f64f39de1faceedf17e09365bf389dbaa9b /challenge-204/0rir
parentf92e84261b474f81c014f4982268d6e2797b66d9 (diff)
downloadperlweeklychallenge-club-9d4724c13ae67bdc3669bf36ee9e75e88f2e1e84.tar.gz
perlweeklychallenge-club-9d4724c13ae67bdc3669bf36ee9e75e88f2e1e84.tar.bz2
perlweeklychallenge-club-9d4724c13ae67bdc3669bf36ee9e75e88f2e1e84.zip
204
with late 202's ch-2.raku
Diffstat (limited to 'challenge-204/0rir')
-rw-r--r--challenge-204/0rir/raku/ch-1.raku55
-rw-r--r--challenge-204/0rir/raku/ch-2.raku89
2 files changed, 144 insertions, 0 deletions
diff --git a/challenge-204/0rir/raku/ch-1.raku b/challenge-204/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..5a118e69f7
--- /dev/null
+++ b/challenge-204/0rir/raku/ch-1.raku
@@ -0,0 +1,55 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ « ␤ » ∴
+use v6.d;
+use lib $?FILE.IO.parent(2).add("lib");
+use Test;
+
+=begin comment
+204-1: Monotonic Array Submitted by: Mohammad S Anwar
+Given an array of integers, find if the given array is Monotonic.
+Print 1 if it is otherwise 0.
+
+An array is Monotonic if it is either monotone increasing or decreasing.
+
+Monotone increasing: for i <= j , nums[i] <= nums[j]
+Monotone decreasing: for i <= j , nums[i] >= nums[j]
+
+Example 1
+Input: @nums = (1,2,2,3)
+Output: 1
+Example 2
+Input: @nums (1,3,2)
+Output: 0
+Example 3
+Input: @nums = (6,5,5,4)
+Output: 1
+=end comment
+
+my @Test =
+ (Empty) => Bool,
+ (1,2,2,3) => True,
+ (1,3,2) => False,
+ (6,5,5,4) => True,
+;
+
+multi monotonic( Empty ) { Bool }
+multi monotonic( $list --> Bool) {
+ my $l = $list.sort.List;
+ $list ~~ $l or $list ~~ $l.reverse;
+}
+
+plan +@Test;
+for @Test -> ( :key($in), :value($exp)) {
+ is monotonic($in), $exp, "$in.raku() -> monotonic $exp.gist()";
+}
+done-testing;
+
+
+print "\n";
+my @num = (1,2,3,4,5,6,7,8,9,10,11,11,11,1,12,13,14,15,15,15,15,16,17,19),
+ (1,2,3,4,5,6,7,8,9,10,11,11,11,12,13,14,15,15,15,15,16,17,19);
+
+for 0..^@num {
+say "Input: @num = @num[$_]
+Output: &monotonic(@num[$_]).Int()";
+}
diff --git a/challenge-204/0rir/raku/ch-2.raku b/challenge-204/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..e9d49fd8d1
--- /dev/null
+++ b/challenge-204/0rir/raku/ch-2.raku
@@ -0,0 +1,89 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ « ␤ » ∴
+use v6.d;
+use lib $?FILE.IO.parent(2).add("lib");
+use Test;
+
+=begin comment
+204-2: Reshape Matrix Submitted by: Mohammad S Anwar
+
+Given a matrix (m x n) and two integers (r) and (c), reshape that matrix in form
+(r x c) with the original value in the given matrix. If you can’t reshape print 0.
+
+Example 1
+Input: [ 1 2 ]
+ [ 3 4 ]
+
+ $matrix = [ [ 1, 2 ], [ 3, 4 ] ]
+ $r = 1
+ $c = 4
+
+Output: [ 1 2 3 4 ]
+Example 2
+Input: [ 1 2 3 ]
+ [ 4 5 6 ]
+
+ $matrix = [ [ 1, 2, 3 ] , [ 4, 5, 6 ] ]
+ $r = 3
+ $c = 2
+
+Output: [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]
+
+ [ 1 2 ]
+ [ 3 4 ]
+ [ 5 6 ]
+Example 3
+Input: [ 1 2 ]
+
+ $matrix = [ [ 1, 2 ] ]
+ $r = 3
+ $c = 2
+
+Output: 0
+=end comment
+
+my @Test =
+ [ [ [1,2],[3,4]], 1, 4, [1,2,3,4] ],
+ [ [ [1,2,3],[4,5,6]], 3, 2, [ [1,2],[3,4],[5,6]] ],
+
+ [ [ 1,2 ], 3, 2, Array ],
+ [ [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18], 3, 5, Array ],
+ [ [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18], 5, 3, Array ],
+
+ [ [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18], 1, 18,
+ [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]
+ ],
+ [ [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18], 2, 9,
+ [[1,2,3,4,5,6,7,8,9],[10,11,12,13,14,15,16,17,18]]
+ ],
+ [ [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18], 3, 6,
+ [[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18]]
+ ],
+ [ [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18], 6, 3,
+ [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18]]
+ ],
+ [ [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18], 9, 2,
+ [[1,2],[3,4],[5,6],[7,8],[9,10],[11,12],[13,14],[15,16],[17,18]]
+ ],
+ [ [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18], 18, 1,
+ [[1,],[2,],[3,],[4,],[5,],[6,],[7,],[8,],[9,],[10,],[11,],[12,],[13,],[14,],[15,],[16,],[17,],[18,]]
+ ],
+;
+
+enum Fields < Matrix Rows Cols Exp >;
+
+multi reshape-matrix( @matrix, $rows, $cols --> Array ) {
+ my @m = @matrix;
+ @m = @m».List.flat;
+ return Array if @m.elems ≠ $rows × $cols;
+ (@matrix».List.flat.rotor( $rows)».Array).Array;
+}
+
+plan +@Test;
+for @Test -> @t {
+ is reshape-matrix(@t[Matrix], @t[Rows], @t[Cols]), @t[Exp];
+}
+done-testing;
+
+
+