From d71ddc7a14553163b23a0e4347e17d5259d8b8ba Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Mon, 2 Oct 2023 10:37:00 +0530 Subject: [PATCH 1/7] add median of matrix --- matrix/median_matrix.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 matrix/median_matrix.py diff --git a/matrix/median_matrix.py b/matrix/median_matrix.py new file mode 100644 index 000000000000..85ca25a19560 --- /dev/null +++ b/matrix/median_matrix.py @@ -0,0 +1,37 @@ +from typing import List + +def median(matrix: List[List[int]]) -> int: + """ + Calculate the median of a sorted matrix. + + Args: + matrix: A 2D matrix of integers. + + Returns: + The median value of the matrix. + + Examples: + >>> matrix = [[1, 3, 5], [2, 6, 9], [3, 6, 9]] + >>> median(matrix) + 5 + + >>> matrix = [[1, 2, 3], [4, 5, 6]] + >>> median(matrix) + 3 + """ + # Flatten the matrix into a 1D list + linear = [num for row in matrix for num in row] + + # Sort the 1D list + linear.sort() + + # Calculate the middle index + mid = (0 + len(linear) - 1) // 2 + + # Return the median + return linear[mid] + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 389eb2214b0fdd2f6e3b763b7b4e66c43c4a9c30 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 05:11:13 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/median_matrix.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/matrix/median_matrix.py b/matrix/median_matrix.py index 85ca25a19560..60f8db1c5753 100644 --- a/matrix/median_matrix.py +++ b/matrix/median_matrix.py @@ -1,5 +1,6 @@ from typing import List + def median(matrix: List[List[int]]) -> int: """ Calculate the median of a sorted matrix. @@ -21,16 +22,17 @@ def median(matrix: List[List[int]]) -> int: """ # Flatten the matrix into a 1D list linear = [num for row in matrix for num in row] - + # Sort the 1D list linear.sort() - + # Calculate the middle index mid = (0 + len(linear) - 1) // 2 - + # Return the median return linear[mid] + if __name__ == "__main__": import doctest From 010e88cc3fc089197ad18ca06facd9cd0ddd7aac Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Mon, 2 Oct 2023 10:44:47 +0530 Subject: [PATCH 3/7] update --- matrix/median_matrix.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/matrix/median_matrix.py b/matrix/median_matrix.py index 60f8db1c5753..8766a4ce02fb 100644 --- a/matrix/median_matrix.py +++ b/matrix/median_matrix.py @@ -1,7 +1,8 @@ -from typing import List +""" +https://en.wikipedia.org/wiki/Median +""" - -def median(matrix: List[List[int]]) -> int: +def median(matrix: list[list[int]]) -> int: """ Calculate the median of a sorted matrix. @@ -22,17 +23,16 @@ def median(matrix: List[List[int]]) -> int: """ # Flatten the matrix into a 1D list linear = [num for row in matrix for num in row] - + # Sort the 1D list linear.sort() - + # Calculate the middle index mid = (0 + len(linear) - 1) // 2 - + # Return the median return linear[mid] - if __name__ == "__main__": import doctest From f320b36021d4d5d07b800161f36b544aa222271a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 05:15:54 +0000 Subject: [PATCH 4/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/median_matrix.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/matrix/median_matrix.py b/matrix/median_matrix.py index 8766a4ce02fb..f84e16419ad6 100644 --- a/matrix/median_matrix.py +++ b/matrix/median_matrix.py @@ -2,6 +2,7 @@ https://en.wikipedia.org/wiki/Median """ + def median(matrix: list[list[int]]) -> int: """ Calculate the median of a sorted matrix. @@ -23,16 +24,17 @@ def median(matrix: list[list[int]]) -> int: """ # Flatten the matrix into a 1D list linear = [num for row in matrix for num in row] - + # Sort the 1D list linear.sort() - + # Calculate the middle index mid = (0 + len(linear) - 1) // 2 - + # Return the median return linear[mid] + if __name__ == "__main__": import doctest From a50e0a21af2e4546e1a60b79941ae016d56557d2 Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Mon, 2 Oct 2023 10:48:10 +0530 Subject: [PATCH 5/7] fix formating --- matrix/median_matrix.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/matrix/median_matrix.py b/matrix/median_matrix.py index f84e16419ad6..95825bb74879 100644 --- a/matrix/median_matrix.py +++ b/matrix/median_matrix.py @@ -2,7 +2,6 @@ https://en.wikipedia.org/wiki/Median """ - def median(matrix: list[list[int]]) -> int: """ Calculate the median of a sorted matrix. @@ -34,7 +33,6 @@ def median(matrix: list[list[int]]) -> int: # Return the median return linear[mid] - if __name__ == "__main__": import doctest From b2a6a74b6b733444b3827d315287cf29c70e6f9b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 05:19:19 +0000 Subject: [PATCH 6/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/median_matrix.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/matrix/median_matrix.py b/matrix/median_matrix.py index 95825bb74879..f84e16419ad6 100644 --- a/matrix/median_matrix.py +++ b/matrix/median_matrix.py @@ -2,6 +2,7 @@ https://en.wikipedia.org/wiki/Median """ + def median(matrix: list[list[int]]) -> int: """ Calculate the median of a sorted matrix. @@ -33,6 +34,7 @@ def median(matrix: list[list[int]]) -> int: # Return the median return linear[mid] + if __name__ == "__main__": import doctest From 19186a6be72c094ed2efa4b7500568465c797115 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 4 Oct 2023 19:17:22 +0200 Subject: [PATCH 7/7] Apply suggestions from code review --- matrix/median_matrix.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/matrix/median_matrix.py b/matrix/median_matrix.py index f84e16419ad6..116e609a587c 100644 --- a/matrix/median_matrix.py +++ b/matrix/median_matrix.py @@ -22,14 +22,11 @@ def median(matrix: list[list[int]]) -> int: >>> median(matrix) 3 """ - # Flatten the matrix into a 1D list - linear = [num for row in matrix for num in row] - - # Sort the 1D list - linear.sort() + # Flatten the matrix into a sorted 1D list + linear = sorted(num for row in matrix for num in row) # Calculate the middle index - mid = (0 + len(linear) - 1) // 2 + mid = (len(linear) - 1) // 2 # Return the median return linear[mid]