Skip to content

Instantly share code, notes, and snippets.

@RafaelBroseghini
Created November 15, 2018 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RafaelBroseghini/22090c5d3f14b26a779bd3e3d93b6bcd to your computer and use it in GitHub Desktop.
Save RafaelBroseghini/22090c5d3f14b26a779bd3e3d93b6bcd to your computer and use it in GitHub Desktop.
Handling Matrices Like a PRO
"""
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