aboutsummaryrefslogtreecommitdiff
path: root/challenge-251/lubos-kolouch/python/ch-2.py
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2024-01-14 14:22:57 +0100
committerLubos Kolouch <lubos@kolouch.net>2024-01-14 14:22:57 +0100
commitae2d092f1d47bcf5a5c4e622ae2d7117e76dfc8a (patch)
tree5a724435600b6d05ef1c6b3da1a2965c8f8d7fee /challenge-251/lubos-kolouch/python/ch-2.py
parent9a485c9bac8e3887b165d67c9aa81d71cdd42f01 (diff)
downloadperlweeklychallenge-club-ae2d092f1d47bcf5a5c4e622ae2d7117e76dfc8a.tar.gz
perlweeklychallenge-club-ae2d092f1d47bcf5a5c4e622ae2d7117e76dfc8a.tar.bz2
perlweeklychallenge-club-ae2d092f1d47bcf5a5c4e622ae2d7117e76dfc8a.zip
feat(challenge-251/lubos-kolouch/perl,python,raku/): Challenge 251 LK Perl Python Raku
Diffstat (limited to 'challenge-251/lubos-kolouch/python/ch-2.py')
-rw-r--r--challenge-251/lubos-kolouch/python/ch-2.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/challenge-251/lubos-kolouch/python/ch-2.py b/challenge-251/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..8983f5e6f0
--- /dev/null
+++ b/challenge-251/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,32 @@
+from typing import List
+
+
+def lucky_number(matrix: list[list[int]]) -> int:
+ """
+ Returns the lucky number in the given matrix, 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.
+
+ Args:
+ matrix: A list of lists of integers representing the matrix.
+
+ Returns:
+ An integer representing the lucky number, or -1 if there is no lucky number in the matrix.
+ """
+ for i in range(len(matrix)):
+ min_row = min(matrix[i])
+ min_index = matrix[i].index(min_row)
+ if all(matrix[j][min_index] <= min_row for j in range(len(matrix))):
+ return min_row
+ return -1
+
+
+# Example usage
+matrix1 = [[3, 7, 8], [9, 11, 13], [15, 16, 17]]
+print(lucky_number(matrix1)) # Output: 15
+
+matrix2 = [[1, 10, 4, 2], [9, 3, 8, 7], [15, 16, 17, 12]]
+print(lucky_number(matrix2)) # Output: 12
+
+matrix3 = [[7, 8], [1, 2]]
+print(lucky_number(matrix3)) # Output: 7