You will check by computer that the median of some points on a line gives the least total distance.
The idea. The total distance from to the points is . The triangle inequality shows it is least at the median: the middle point of the sorted list, or any point between the two middle ones.
Your tasks.
total(points, x).median(points). For an odd number of points return the middle one of the sorted list; for an even number return the mean of the two middle ones.grid_best(points, lo, hi, steps): the smallest value of total on steps + 1 equally spaced points from lo to hi.Run your code once to unlock the worked solution.
Have you tried it?
Do the tasks in the editor first, and run the checker after each one. Its hints help when a check fails. When you are ready, compare your code with this solution.
Log in to join the discussion.
Loading…