Click4Ai

40.

Medium

Write a function inverse_2x2(matrix) that computes the inverse of a 2×2 matrix. Return "Singular" if the matrix is not invertible (det = 0).

**Formula:** For [[a,b],[c,d]], inverse = (1/det) * [[d, -b], [-c, a]]

Example:

Input: matrix = [[4, 7], [2, 6]]

Output: [[0.6, -0.7], [-0.2, 0.4]]

**Explanation:** det = 24-14 = 10, inverse = (1/10)*[[6,-7],[-2,4]]

Constraints:

  • Input is always a 2×2 matrix
  • Return "Singular" if det = 0
  • Return list of lists with floats rounded to 4 decimal places
  • Test Cases

    Test Case 1
    Input: [[4, 7], [2, 6]]
    Expected: [[0.6, -0.7], [-0.2, 0.4]]
    Test Case 2
    Input: [[1, 0], [0, 1]]
    Expected: [[1.0, 0.0], [0.0, 1.0]]
    Test Case 3
    Input: [[2, 1], [5, 3]]
    Expected: [[3.0, -1.0], [-5.0, 2.0]]
    + 2 hidden test cases