Closest to the Target

Python for AIMedium+10 XP+10

Closest to the Target

In a guessing game, the player whose guess is nearest the secret number wins.

Write by_closeness(guesses, target). It returns the guesses sorted from closest to the target to furthest.

  • "Closeness" means distance in either direction: guesses of 8 and 12 are equally close to 10.
  • Guesses that are equally close keep the order they had in the original list.

Examples

guesses = [15, 9, 2]
target = 10
returns [9, 15, 2]
guesses = [12, 8]
target = 10
returns [12, 8]

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