diff options
| author | rir <rirans@comcast.net> | 2024-01-14 21:31:33 -0500 |
|---|---|---|
| committer | rir <rirans@comcast.net> | 2024-01-14 21:31:33 -0500 |
| commit | dc1f0ba3a53d39db64e7d3676bd0adebe2a98c75 (patch) | |
| tree | aa0178c57d7540e907ad1d068c15914fbed47041 | |
| parent | 0b41294e70fc0f3c38745c08ecf4aff0639598db (diff) | |
| download | perlweeklychallenge-club-dc1f0ba3a53d39db64e7d3676bd0adebe2a98c75.tar.gz perlweeklychallenge-club-dc1f0ba3a53d39db64e7d3676bd0adebe2a98c75.tar.bz2 perlweeklychallenge-club-dc1f0ba3a53d39db64e7d3676bd0adebe2a98c75.zip | |
251-2
| -rwxr-xr-x | challenge-251/0rir/raku/ch-2.raku | 42 |
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; |
