The problem
Two straight pieces of track run from a to b and from c to d. Return 0 if they have no point in common, 1 if they have exactly one, and 2 if they share more than one point (they lie on one line and overlap along a stretch).
meet((0, 0), (4, 4), (0, 4), (4, 0)) # 1: they cross at (2, 2)
meet((0, 0), (2, 2), (2, 2), (4, 0)) # 1: a shared end
meet((0, 0), (3, 3), (1, 1), (5, 5)) # 2: they overlap
meet((0, 0), (1, 1), (2, 2), (3, 3)) # 0: one line, apart
A piece may be a single point (both its ends equal). Pieces may be upright. Coordinates can be as large as 10¹⁷, where floats are not exact.
Builds on GEOM-2 Orientation and intersection.
Continue on a laptop
Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.
Need a hint?0 of 3 opened
- Hint 1Nudge
- Hint 2Approach Open hint 1 first
- Hint 3Pseudocode Open hint 2 first
- Worked solutionlast resort Opens after all 3 hints
Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.
| # | Test | Result | Reads · time |
|---|---|---|---|
| 1 | A crossing | Not run | |
| 2 | Apart | Not run | |
| 3 | The lines cross, the pieces do not | Not run | |
| 4 | Parallel | Not run | |
| 5 | Touching ends | Not run | |
| 6 | An end on the middle | Not run | |
| 7 | Almost touching | Not run | |
| 8 | On one line, apart | Not run | |
| 9 | On one line, end to end | Not run | |
| 10 | On one line, overlapping | Not run | |
| 11 | One inside the other | Not run | |
| 12 | Upright and overlapping | Not run | |
| 13 | Upright and apart | Not run | |
| 14 | A single point on a piece | Not run | |
| 15 | Two single points | Not run | |
| 16 | A point off the line | Not run | |
| 17 | Far from floats | Not run |