Created
November 15, 2018 17:33
-
-
Save RafaelBroseghini/22090c5d3f14b26a779bd3e3d93b6bcd to your computer and use it in GitHub Desktop.
Handling Matrices Like a PRO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Given a N x N matrix, let us extract the rows and columns. | |
This function takes a matrix as parameter and returns a | |
tuple of rows and columns. Both rows and columns are also matrices. (lists of lists) | |
NOTE: matrix passed in as parameter must have N rows and N columns for this algorithm | |
to work. | |
""" | |
def get_rows_and_columns(matrix: list) -> tuple: | |
size_of_row, size_of_matrix = len(matrix[0]), len(matrix) | |
rows, columns = list(matrix), [] | |
for column in range(size_of_row): | |
c = [] | |
for row in range(size_of_matrix): | |
c.append(matrix[row][column]) | |
columns.append(c) | |
return rows, columns | |
if __name__ == "__main__": | |
rows, columns = get_rows_and_columns([[1,2,3],[4,5,6]])) | |
print(rows) | |
print(columns) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment