← back to unit
Medium
# searching medium
Closest to a Target Number
The carnival guessing game: whoever's guess lands closest to the secret number wins the giant panda. 🐼
You get a list of guesses (at least one) and the secret number. Print the winning guess. On a tie, the earlier guess wins.
Example:
Input: guesses = [10, 25, 18, 30], secret = 20
Output: 18
The distances to 20 are 10, 5, 2, 10 — 18's distance of 2 is the smallest.
💡 need a hint?
srch-closest-to-target.py🔒 given lines are locked — write your code in between
loading...