An old map gives a path as a list of steps (paces, bearing): walk paces paces in the direction bearing, in degrees, measured anticlockwise from east. So bearing 0 is east, 90 north, 180 west and 270 south (bearings may also be negative or larger than 360; -90 is south too).
Put the start at the origin of the complex plane, with east along the real axis and north along the imaginary axis. One pace in the direction \theta (in radians) is the unit arrow given by Euler's formula
which is cmath.exp(1j * theta) in Python (or complex(math.cos(theta), math.sin(theta))). A step of L paces is L\,e^{i\theta}, and a path is the sum of its steps. math.radians(d) turns degrees into radians.
Write treasure_spot(steps) that returns where the path ends, as a complex number.
Examples
Input: steps = [(3, 0), (4, 90)]
Output: (3+4j)
Explanation: 3 paces east, then 4 paces north.
Input: steps = [(2, 45), (2, 135), (1, 270)]
Output: 1.8284271247461903j
Explanation: 2 paces north-east and 2 paces north-west add up to 2*sqrt(2) = 2.828... paces north;
1 pace south leaves 1.828... paces north of the start.
Values like 3.8e-17 where the exact answer is 0 count as correct. An empty path ends at the start, 0.
Constraints
- answers are compared with a tolerance of
1e-6; do not round
Goals
- Turn a length and an angle into a complex number with Euler's formula
- Convert degrees to radians before calling cmath or math
- Add arrows to follow a path