aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-05-28 23:27:32 +0100
committerGitHub <noreply@github.com>2023-05-28 23:27:32 +0100
commitfefe951e19cc55c9bcc7395d708e63739218964b (patch)
treea722a725dc1332a0bac3aa425dc49c416d4d9d7d
parent6890be063d8bf4a9e2314b328bc95eb1ba567654 (diff)
parentb65bf57cacc19fd35eca9cfe193d9c32bebeb999 (diff)
downloadperlweeklychallenge-club-fefe951e19cc55c9bcc7395d708e63739218964b.tar.gz
perlweeklychallenge-club-fefe951e19cc55c9bcc7395d708e63739218964b.tar.bz2
perlweeklychallenge-club-fefe951e19cc55c9bcc7395d708e63739218964b.zip
Merge pull request #8151 from BarrOff/barroff-217
feat: add solutions for challenge 217 from BarrOff
-rw-r--r--challenge-217/barroff/perl/ch-1.pl26
-rw-r--r--challenge-217/barroff/raku/ch-1.raku22
-rw-r--r--challenge-217/barroff/raku/ch-2.raku36
3 files changed, 84 insertions, 0 deletions
diff --git a/challenge-217/barroff/perl/ch-1.pl b/challenge-217/barroff/perl/ch-1.pl
new file mode 100644
index 0000000000..fcde17d417
--- /dev/null
+++ b/challenge-217/barroff/perl/ch-1.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/env perl
+
+use v5.36;
+use strict;
+use warnings;
+
+sub sorted_matrix (@s) {
+ my @as_vector;
+ map append( @as_vector, @$_ ), @s;
+ my @sorted = sort @as_vector;
+ return $sorted[2];
+}
+
+#| Run test cases
+sub MAIN() {
+ use Test2::V0 qw(is plan);
+ plan 3;
+
+ is sorted_matrix( ( [ 3, 1, 2 ], [ 5, 2, 4 ], [ 0, 1, 3 ] ) ), 1,
+ "works for ([3, 1, 2], [5, 2, 4], [0, 1, 3])";
+ is sorted_matrix( ( [ 2, 1 ], [ 4, 5 ] ) ), 4, "works for ([2, 1], [4, 5])";
+ is sorted_matrix( ( [ 1, 0, 3 ], [ 0, 0, 0 ], [ 1, 2, 1 ] ) ), 0,
+ "works for ([1, 0, 3], [0, 0, 0], [1, 2, 1])";
+}
+
+MAIN();
diff --git a/challenge-217/barroff/raku/ch-1.raku b/challenge-217/barroff/raku/ch-1.raku
new file mode 100644
index 0000000000..63bf6e5327
--- /dev/null
+++ b/challenge-217/barroff/raku/ch-1.raku
@@ -0,0 +1,22 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub sorted-matrix(@matrix where @matrix.elems == @matrix.all --> Int) {
+ my @sorted-numbers = sort map(&slip, @matrix);
+ return @sorted-numbers[2];
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 3;
+
+ is sorted-matrix(([3, 1, 2], [5, 2, 4], [0, 1, 3])), 1, "works for ([3, 1, 2], [5, 2, 4], [0, 1, 3])";
+ is sorted-matrix(([2, 1], [4, 5])), 4, "works for ([2, 1], [4, 5])";
+ is sorted-matrix(([1, 0, 3], [0, 0, 0], [1, 2, 1])), 0, "works for ([1, 0, 3], [0, 0, 0], [1, 2, 1])";
+}
+
+multi sub MAIN() {
+ MAIN('test');
+}
diff --git a/challenge-217/barroff/raku/ch-2.raku b/challenge-217/barroff/raku/ch-2.raku
new file mode 100644
index 0000000000..eab82b97e8
--- /dev/null
+++ b/challenge-217/barroff/raku/ch-2.raku
@@ -0,0 +1,36 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub compare-same-start(Int $a, Int $b --> Order) {
+ my Str @a_chars = $a.comb;
+ return @a_chars[0] cmp @a_chars[$b.chars];
+}
+
+sub compare-numbers(Int $a, Int $b --> Order) {
+ return compare-same-start($a, $b) if $a.starts-with($b);
+ return compare-same-start($b, $a) if $b.starts-with($a);
+ return Str($a) cmp Str($b);
+}
+
+sub max-number(Int @numbers --> Int) {
+ my @sorted-numbers = sort(&compare-numbers, @numbers).reverse;
+ return Int(@sorted-numbers.join);
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 5;
+
+ is max-number(Array[Int].new(1, 23)), 231, "works for (1, 23)";
+ is max-number(Array[Int].new(10, 3, 2)), 3210, "works for (10, 3, 2)";
+ is max-number(Array[Int].new(31, 2, 4, 10)), 431210, "works for (31, 2, 4, 10)";
+ is max-number(Array[Int].new(5, 11, 4, 1, 2)), 542111, "works for (5, 11, 4, 1, 2)";
+ is max-number(Array[Int].new(1, 10)), 110, "works for (1, 10)";
+}
+
+multi sub MAIN() {
+ MAIN('test');
+}
+