from itertools import permutations from fractions import Fraction A = [[1, 1, 1], [2, -1, 1], [1, 2, -1]] b = [6, 3, 2] def det(matrix): total = 0 for p in permutations(range(3)): inversions = sum(p[i] > p[j] for i in range(3) for j in range(i + 1, 3)) term = (-1) ** inversions for i in range(3): term *= matrix[i][p[i]] total += term return total denominator = det(A) assert denominator == 7 answer = [] for col in range(3): replaced = [row[:] for row in A] for r in range(3): replaced[r][col] = b[r] answer.append(str(Fraction(det(replaced), denominator))) result = {"solution": answer} import json print(json.dumps(result, ensure_ascii=False, sort_keys=True))