Click4Ai

30.

Easy

Write a function column_means(matrix) that returns a list where each element is the mean of the corresponding column in the matrix. Do not use np.mean().

Example:

Input: matrix = [[1, 2, 3],

[4, 5, 6],

[7, 8, 9]]

Output: [4.0, 5.0, 6.0]

**Explanation:** Col 0: (1+4+7)/3=4.0, Col 1: (2+5+8)/3=5.0, Col 2: (3+6+9)/3=6.0

Constraints:

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

    Test Case 1
    Input: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    Expected: [4.0, 5.0, 6.0]
    Test Case 2
    Input: [[10, 20], [30, 40]]
    Expected: [20.0, 30.0]
    Test Case 3
    Input: [[5, 10, 15]]
    Expected: [5.0, 10.0, 15.0]
    + 2 hidden test cases