MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / transpose

Function transpose

matrix/matrix_operation.py:95–113  ·  view source on GitHub ↗

>>> transpose([[1,2],[3,4]]) # doctest: +ELLIPSIS >> transpose([[1,2],[3,4]], return_map=False) [[1, 3], [2, 4]] >>> transpose([1, [2, 3]]) Traceback (most recent call last): ... TypeError: Expected a matrix, got int/list instead

(
    matrix: list[list[int]], return_map: bool = True
)

Source from the content-addressed store, hash-verified

93
94
95def transpose(
96 matrix: list[list[int]], return_map: bool = True
97) -> list[list[int]] | map[list[int]]:
98 """
99 >>> transpose([[1,2],[3,4]]) # doctest: +ELLIPSIS
100 <map object at ...
101 >>> transpose([[1,2],[3,4]], return_map=False)
102 [[1, 3], [2, 4]]
103 >>> transpose([1, [2, 3]])
104 Traceback (most recent call last):
105 ...
106 TypeError: Expected a matrix, got int/list instead
107 """
108 if _check_not_integer(matrix):
109 if return_map:
110 return map(list, zip(*matrix))
111 else:
112 return list(map(list, zip(*matrix)))
113 raise TypeError("Expected a matrix, got int/list instead")
114
115
116def minor(matrix: list[list[int]], row: int, column: int) -> list[list[int]]:

Callers 1

inverseFunction · 0.70

Calls 1

_check_not_integerFunction · 0.85

Tested by

no test coverage detected