Click4Ai

41.

Medium

Write a function solve_system(A, b) that solves a 2×2 system of linear equations Ax = b using Cramer's rule or the inverse method.

**Cramer's Rule:** x = det(Ax)/det(A), y = det(Ay)/det(A) where Ax replaces column 1 with b, Ay replaces column 2 with b.

Example:

Input: A = [[2, 3], [4, 5]], b = [8, 14]

Output: [1.0, 2.0]

**Explanation:** 2x + 3y = 8 and 4x + 5y = 14 → x=1, y=2

Constraints:

  • A is always 2×2, b has 2 elements
  • System has a unique solution (det(A) ≠ 0)
  • Return a list of floats
  • Test Cases

    Test Case 1
    Input: [[2, 3], [4, 5]], [8, 14]
    Expected: [1.0, 2.0]
    Test Case 2
    Input: [[1, 0], [0, 1]], [5, 3]
    Expected: [5.0, 3.0]
    Test Case 3
    Input: [[1, 1], [1, -1]], [10, 4]
    Expected: [7.0, 3.0]
    + 2 hidden test cases