/
ssmih
/
Python
Обзор
Документация
Войти
/
ssmih
/
Python
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
matrix/median_matrix.py
38 строк
738 B
Bama Charan Chhandogi
add median of matrix (#9363)
04 окт 2023, 20:21
Не верифицирован
04 окт 2023, 20:21
922d6a8
Код
Авторство
О чём код?
""" https://en.wikipedia.org/wiki/Median """ 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 sorted 1D list linear = sorted(num for row in matrix for num in row) # Calculate the middle index mid = (len(linear) - 1) // 2 # Return the median return linear[mid] if __name__ == "__main__": import doctest doctest.testmod()