Click4Ai

25.

Easy

Write a function flatten_matrix(matrix) that converts a 2D matrix into a 1D list by concatenating all rows. Do not use np.flatten() or np.ravel().

Example:

Input: matrix = [[1, 2, 3], [4, 5, 6]]

Output: [1, 2, 3, 4, 5, 6]

Constraints:

  • Matrix dimensions: 1 <= rows, cols <= 100
  • Return a flat list
  • Test Cases

    Test Case 1
    Input: [[1, 2, 3], [4, 5, 6]]
    Expected: [1, 2, 3, 4, 5, 6]
    Test Case 2
    Input: [[1, 2], [3, 4], [5, 6]]
    Expected: [1, 2, 3, 4, 5, 6]
    Test Case 3
    Input: [[7]]
    Expected: [7]
    + 2 hidden test cases