diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-15 02:50:56 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-15 02:50:56 +0000 |
| commit | f89b725f3ae9d78e0bc206e5bf5e1b8a3189c7bc (patch) | |
| tree | 0fb8c26468adeef84ad96bd0b41e9921cb7d4701 | |
| parent | 42ad71b7bfa4df5b4927fc4cc293699a6e847334 (diff) | |
| parent | dc1f0ba3a53d39db64e7d3676bd0adebe2a98c75 (diff) | |
| download | perlweeklychallenge-club-f89b725f3ae9d78e0bc206e5bf5e1b8a3189c7bc.tar.gz perlweeklychallenge-club-f89b725f3ae9d78e0bc206e5bf5e1b8a3189c7bc.tar.bz2 perlweeklychallenge-club-f89b725f3ae9d78e0bc206e5bf5e1b8a3189c7bc.zip | |
Merge pull request #9401 from 0rir/251
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; |
