Every Jumper Against Every Other

Data with PythonMedium+20 XP+20

Every Jumper Against Every Other

At a long-jump trial, each athlete's best jump is recorded in whole centimetres. The coach wants every athlete compared with every other one in a single grid, where

gaps[i][j]=jumps[i]−jumps[j]\text{gaps}[i][j] = \text{jumps}[i] - \text{jumps}[j]gaps[i][j]=jumps[i]−jumps[j]

is how much further athlete iii jumped than athlete jjj — negative if iii jumped shorter. Row iii is athlete iii's view of the whole field.

Jumps only a few centimetres apart get argued over, so athlete iii only clearly beats athlete jjj when gaps[i][j]\text{gaps}[i][j]gaps[i][j] is strictly greater than margin.

Task: write head_to_head(jumps, margin) returning a tuple (gaps, clear_wins):

  • gaps — the whole grid as a list of lists: one row per athlete and one column per athlete, both in the order given;
  • clear_wins — for each athlete, in order, how many others they clearly beat.

margin is never negative.

Example

Athlete 0 out-jumped athlete 1 by 25 cm — more than 20, so it counts — but fell 28 cm short of athlete 2. Athlete 2 clearly beat both of the others.

A grid of every sample against every other sample is the starting point whenever you ask which examples sit closest together. This is the one-feature version.

Examples

jumps = [612, 587, 640]
margin = 20
returns ([[0, 25, -28], [-25, 0, -53], [28, 53, 0]], [1, 0, 2])
jumps = [598, 615, 598, 571]
margin = 0
returns ([[0, -17, 0, 27], [17, 0, 17, 44], [0, -17, 0, 27], [-27, -44, -27, 0]], [1, 3, 1, 0])

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run