Two cameras photograph the same arrangement of arrows, each arrow stored as a complex number. One camera may be mounted turned by some angle \varphi, which multiplies every number by the same unit factor c = e^{i\varphi} (a complex number with |c| = 1). Turning keeps every length: a camera that zooms, which would multiply by a factor of another length, does not count.
Write same_up_to_unit(a, b) that returns True when there is one complex number c with abs(c) == 1 such that b[k] == c * a[k] for every index k (up to rounding), and False otherwise. In particular:
- lists of different lengths are never the same;
- two lists of zeros of the same length (and two empty lists) are the same, with
c = 1; - a zero must face a zero:
[1, 0]and[1, 1]are not the same.
Later in this path, two quantum states that differ only like this will count as the same state.
The tests use a helper, turned(xs, phi), that returns [cmath.exp(1j * phi) * x for x in xs]. You can call it with Run too.
Examples
Input: a = [1, 1j, 0], b = [1j, -1, 0]
Output: True
Explanation: c = 1j turns 1 into 1j and 1j into -1, and keeps 0 at 0.
Input: a = [1, 1j], b = [2j, -2]
Output: False
Explanation: b = 2j * a, but |2j| = 2: that camera zoomed.
Input: a = [1, 1], b = [1, -1]
Output: False
Explanation: the first arrow needs c = 1 and the second c = -1; one factor must fit all.
Constraints
0 <= len(a), len(b) <= 1000, and every entry has modulus at most 100- the tests are never borderline: when the answer is
True,b[k]andc * a[k]agree to within1e-9; when it isFalse, every unit factorcmisses some entry by more than0.001. A tolerance such as1e-6is safe.
Goals
- Decide whether two lists differ by one common factor of modulus 1
- Find the candidate factor from a safe entry instead of dividing by 0
- Compare floating-point complex numbers with a tolerance