Added mat_flat module utility file

This commit is contained in:
Donald Calloway 2025-10-02 09:47:18 -07:00
parent ccacab51ea
commit 7dd801985e

19
mat_flat.py Normal file
View File

@ -0,0 +1,19 @@
from typing import List
"""
Flattens a 2D list (matrix) into a 1D list.
Args:
M (List[List]): A two-dimensional list to be flattened.
Returns:
List: A one-dimensional list containing all elements of the input matrix in row-major order.
Example:
>>> flatten([[1, 2], [3, 4]])
[1, 2, 3, 4]
NOTE: If you add the mat_flat module to a project, call it with mat_flat.flatten(M).
"""
def flatten(M: List[List])-> List:
flat = [elem for row in M for elem in row]
return flat