aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-204/barroff/raku/ch-1.raku33
-rw-r--r--challenge-204/barroff/raku/ch-2.raku26
2 files changed, 59 insertions, 0 deletions
diff --git a/challenge-204/barroff/raku/ch-1.raku b/challenge-204/barroff/raku/ch-1.raku
new file mode 100644
index 0000000000..e29aef5496
--- /dev/null
+++ b/challenge-204/barroff/raku/ch-1.raku
@@ -0,0 +1,33 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub order-pairs(Int @list where @list.elems > 1 --> List) {
+ (@list[1..*] Z<=> @list[0..*-1]).list;
+}
+
+sub is-monotonic(Order @comparisons --> Bool) {
+ my Junction $junc = @comparisons.any;
+ (More == $junc and Less == $junc) ?? False !! True;
+}
+
+sub monotonic-array(Int @elements --> Int) {
+ my Order @orders = order-pairs(@elements);
+ return is-monotonic(@orders) ?? 1 !! 0;
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 3;
+
+ is monotonic-array(Array[Int].new([1, 2, 2, 3])), 1, 'works for (1, 2, 2, 3)';
+ is monotonic-array(Array[Int].new([1, 3, 2])), 0, 'works for (1, 3, 2)';
+ is monotonic-array(Array[Int].new([6, 5, 5, 4])), 1, 'works for (6, 5, 5, 4)';
+}
+
+#| Take user provided list like 1 2 2 3
+multi sub MAIN(*@elements where all(@elements) ~~ /^<[+-]>?<[0..9]>+$/) {
+ my Int @int_elements = @elements;
+ monotonic-array(@int_elements) ?? say 1 !! say 0;
+}
diff --git a/challenge-204/barroff/raku/ch-2.raku b/challenge-204/barroff/raku/ch-2.raku
new file mode 100644
index 0000000000..f48bafd228
--- /dev/null
+++ b/challenge-204/barroff/raku/ch-2.raku
@@ -0,0 +1,26 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub can-reshape(List $list, Int $rows, Int $columns --> Bool) {
+ $list.elems == $rows × $columns;
+}
+
+sub reshape-matrix(List $list, Int $rows where $rows ≥ 1, Int $columns where $columns ≥ 1 --> List) {
+ my List $flat-list = $list.flat.list;
+ my List $result = ();
+ if can-reshape($flat-list, $rows, $columns) {
+ $result = ( $flat-list[($_ × $columns) ..^ ($_ + 1) × $columns] for 0 ..^ $rows );
+ }
+ say '0' unless $result;
+ return $result;
+}
+
+sub MAIN {
+ use Test;
+ plan 3;
+
+ is reshape-matrix(((1, 2), (3, 4)), 1, 4), (1, 2, 3, 4), 'matrix ((1, 2), (3, 4))';
+ is reshape-matrix(((1, 2, 3), (4, 5, 6)), 3, 2), ((1, 2), (3, 4), (5, 6)), 'matrix ((1, 2, 3), (4, 5, 6))';
+ is reshape-matrix(((1, 2)), 3, 2), (), 'matrix ((1, 2))';
+}