diff options
| author | Scott Sotka <ssotka@barracuda.com> | 2024-01-12 13:27:57 -0800 |
|---|---|---|
| committer | Scott Sotka <ssotka@barracuda.com> | 2024-01-12 13:27:57 -0800 |
| commit | 297d90bbf46708d255d3cb09b84a992f84150dbe (patch) | |
| tree | 4ad2299e668c6d034a2e5fb1a492cdd86a5112c2 | |
| parent | e276ea6e66bf75d2793370f9dd677082d9f489ab (diff) | |
| download | perlweeklychallenge-club-297d90bbf46708d255d3cb09b84a992f84150dbe.tar.gz perlweeklychallenge-club-297d90bbf46708d255d3cb09b84a992f84150dbe.tar.bz2 perlweeklychallenge-club-297d90bbf46708d255d3cb09b84a992f84150dbe.zip | |
CH-251 part 2, lucky numbers
| -rwxr-xr-x | challenge-251/bn-ssotka/raku/ch-2.raku | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/challenge-251/bn-ssotka/raku/ch-2.raku b/challenge-251/bn-ssotka/raku/ch-2.raku new file mode 100755 index 0000000000..7dc69e08ce --- /dev/null +++ b/challenge-251/bn-ssotka/raku/ch-2.raku @@ -0,0 +1,38 @@ +#!/usr/bin/env raku + +# You are given a m x n matrix of distinct numbers. + +# Write a script to return the lucky number, if there is one, or -1 if not. + +# A lucky number is an element of the matrix such that it is +# the minimum element in its row and maximum in its column. + +sub get-lucky (@matrix) { + my @row_mins = @matrix.map: *.min; # get the mins of each row + my @rotated_matrix = [Z] @matrix; # rotate the matrix + my @col_maxs = @rotated_matrix.map: *.max; # get the max of each of the rotated rows + + my @lucky = @row_mins ∩ @col_maxs; # get the intersection of the two + return @lucky ?? @lucky[0] !! -1; +} +my @test-matricies = ( + ( + [ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17] + ), + ( + [ 1, 10, 4, 2], + [ 9, 3, 8, 7], + [15, 16, 17, 12] + ), + ( + [7 ,8], + [1 ,2] + ) +); + +for @test-matricies -> @matrix { + say @matrix; + say get-lucky(@matrix); +} |
