Closest pair
Inputs
values is a list of 2 to 12 whole numbers, each from -50 to 50.
Your tasks
- Set
bestto the gap between the first two numbers (ignore the sign). - Use nested loops to try every pair of positions
i < j. If the gap betweenvalues[i]andvalues[j]is smaller thanbest, replacebest. - Complete
closestGapso it returnsbest. Return it, do not print it.
Example
With values = [10, 4, 21, 6], closestGap returns 2.
Rules
- The gap is never negative.
valuesalways has at least 2 numbers.
Test data
TEST DATA 1: values is [10, 4, 21, 6]
TEST DATA 2: values is [3, 3]
Practice focus
Nested loops compare every pair and build rows. The slow, simple answer to most problems is two loops. Knowing it is what lets you see how to beat it.