One small number tells you whether a 2×2 matrix can be undone.
What to build
Print the determinant of a 2×2 matrix and say whether the matrix is invertible.
Requirements
- Read the two rows of the matrix.
- Compute a·d − b·c.
- Print
invertible yesunless the determinant is 0.
Input
Two lines: a b and c d, whole numbers.
Output
Two lines: det D (a whole number) and invertible yes or invertible no.
Constraints
All numbers are between -1000 and 1000.
Example
Input:
4 7
2 6
Output:
det 10
invertible yes
Why it works
The determinant is the signed area of the parallelogram formed by the matrix's columns. If it is 0 the parallelogram is flat, the matrix squashes the plane onto a line or a point, and no inverse can undo that.
Examples
4 7 2 6
det 10 invertible yes
1 2 2 4
det 0 invertible no
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
The determinant of [[a, b], [c, d]] is a·d − b·c.
Hint 2
The matrix has an inverse exactly when the determinant is not 0.
Hint 3
Whole numbers only, so print the determinant as an integer.
Solution
Show one correct solution (try it yourself first)
a, b = [int(v) for v in input().split()]
c, d = [int(v) for v in input().split()]
det = a * d - b * c
print("det", det)
print("invertible", "no" if det == 0 else "yes")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step