The problem
A map has straight pieces of track, each given by its two ends. Two pieces meet if they share any point: they cross, touch, share an end or overlap. Return the positions (i, j) of any two different pieces that meet, or None if no two meet. When several pairs meet, any one of them is accepted; the tests check that the two pieces you name really share a point.
any_crossing([((0, 0), (4, 4)), ((0, 4), (4, 0))]) # (0, 1)
any_crossing([((0, 0), (1, 0)), ((2, 0), (3, 0))]) # None
any_crossing([((0, 0), (2, 2)), ((2, 2), (4, 0))]) # (0, 1)
Each piece has two different ends, given in either order. Pieces may be upright. Coordinates are whole numbers up to about 3 million.
Limits
- Up to 20,002 pieces. The big tests have 4 seconds each.
- Checking every pair is 200 million pair checks, far too slow.
Builds on GEOM-4 Sweep line.
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 | An X | Not run | |
| 2 | Apart | Not run | |
| 3 | One piece | Not run | |
| 4 | Touching ends | Not run | |
| 5 | Neighbours after a removal | Not run | |
| 6 | An upright piece | Not run | |
| 7 | Upright pieces on one line | Not run | |
| 8 | Upright pieces that touch | Not run | |
| 9 | A shared start | Not run | |
| 10 | Stacked, no crossing | Not run | |
| 11 | Overlapping on one line | Not run | |
| 12 | 20,000 pieces, none meet | Not run | |
| 13 | A crossing at the far end | Not run |