aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-251/0rir/raku/ch-2.raku42
1 files changed, 21 insertions, 21 deletions
diff --git a/challenge-251/0rir/raku/ch-2.raku b/challenge-251/0rir/raku/ch-2.raku
index 329adadc43..84ba81fe66 100755
--- a/challenge-251/0rir/raku/ch-2.raku
+++ b/challenge-251/0rir/raku/ch-2.raku
@@ -5,8 +5,6 @@ use v6.e.PREVIEW;
use Math::Matrix;
use Test;
-say %*ENV<VERSION>;
-
=begin comment
251-2: Lucky Numbers Submitted by: Mohammad S Anwar
You are given a m x n matrix of distinct numbers.
@@ -35,46 +33,48 @@ Output: 7
my @Test =
+ -1, [],
+ 1, [ 1, 2, 3 ],
+ 1, [ [ 1, 2, 3], ],
12, [ [ 1, 10, 4, 2],
[ 9, 3, 8, 7],
[15, 16, 17, 12] ],
15, [ [ 3, 7, 8],
[ 9, 11, 13],
[15, 16, 17] ],
- 7, [ [7 ,8],
- [1 ,2] ],
+ 7, [ [7 ,8],
+ [1 ,2] ],
+ -1, [ [ 1,2,3 ],
+ [ 7,5,6 ],
+ [ 4,8,9 ], ],
;
plan @Test ÷ 2;
sub ____ (+@list) { gather @list.deepmap: *.take } # move to Flatland
-the-lucky-number-is();
+# is a one-dim array a matrix?
+multi the-lucky-number-is( @a where * ~~ Empty) { -1 }
+multi the-lucky-number-is( @a where (*.end == 0 and *[0] !~~ Array)) { @a.min }
+multi the-lucky-number-is( @a where (*.end == 1 and *[0] ~~ Array)) {
+ @a[0].min
+}
-sub the-lucky-number-is( @a = @Test[1] --> Int) {
+multi the-lucky-number-is( @a = @Test[1] --> Int) {
my @w = @a.&____.sort;
-say @w.raku;
die 'Non-distinct data' unless @w.sort eqv @w.unique.sort;
- my @lucky;
-
- my @row-min = @a.map( *.min( :p)[0]); # ??????
- for @row-min -> $p {
-say $p;
-say "A col-max ", (@a[ 0..^@a, $p.key]).max;
- my $col-max = @a[ 0..^@a, $p.key].max;
- @lucky.push( $p.value) if $col-max == $p.value;
- }
- say @lucky.raku;
- return @lucky;
+ my @min = @a.map: *.min;
+ my @max = do for 0..^@a[0] -> $col { @a[ 0..^@a ;$col].max; }
+ my $ret = @min ∩ @max;
+ return -1 if $ret ~~ Set.new();
+ $ret.keys[0];
}
-=finish
for @Test -> $exp, $in {
- is the-lucky-number-is($in), $exp, "$exp <- $in";
+ is the-lucky-number-is($in), $exp, "$exp <- $in.raku()";
}
done-testing;
-my @X = X;
exit;