Click4Ai

21.

Easy

Write a function reshape_array(arr, rows, cols) that reshapes a 1D numpy array into a 2D array with the given dimensions. You may use np.reshape().

Example:

Input: arr = [1, 2, 3, 4, 5, 6], rows = 2, cols = 3

Output: [[1, 2, 3],

[4, 5, 6]]

Constraints:

  • rows * cols must equal len(arr)
  • Return a numpy 2D array
  • Return the string "Error" if reshape is not possible
  • Test Cases

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