aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-01-12 22:40:27 +0000
committerGitHub <noreply@github.com>2024-01-12 22:40:27 +0000
commita66ff29f623858ec6392d9a39c2ed17741aeec93 (patch)
tree4ad2299e668c6d034a2e5fb1a492cdd86a5112c2
parente276ea6e66bf75d2793370f9dd677082d9f489ab (diff)
parent297d90bbf46708d255d3cb09b84a992f84150dbe (diff)
downloadperlweeklychallenge-club-a66ff29f623858ec6392d9a39c2ed17741aeec93.tar.gz
perlweeklychallenge-club-a66ff29f623858ec6392d9a39c2ed17741aeec93.tar.bz2
perlweeklychallenge-club-a66ff29f623858ec6392d9a39c2ed17741aeec93.zip
Merge pull request #9386 from bn-ssotka/ch-251-p2
CH-251 part 2, lucky numbers
-rwxr-xr-xchallenge-251/bn-ssotka/raku/ch-2.raku38
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);
+}