w3resource

Python: Find the indices of the closest pair from given a list of numbers

Python Programming Puzzles: Exercise-66 with Solution

Write a Python program to find the indices of the closest pair from a list of numbers.

Input: [1, 7, 9, 2, 10]
Output:
[0, 3]

Input: [1.1, 4.25, 0.79, 1.0, 4.23]
Output:
[4, 1]

Input: [0.21, 11.3, 2.01, 8.0, 10.0, 3.0, 15.2]
Output:
[2, 5]

Visual Presentation:

Python: Find the indices of the closest pair from given a list of numbers.

Sample Solution:

Python Code:

# License: https://bit.ly/3oLErEI

def test(nums):
    # Initialize variables to store indices of the closest pair and the closest distance
    closest_inds = None
    closest_dist = None
    
    # Iterate through each element in the list
    for ind, num in enumerate(nums):
        # Compare each element with every other element in the list
        for other_ind, num2 in enumerate(nums):
            # Check if the elements are distinct and calculate the absolute difference
            if num != num2 and ((closest_dist is None) or abs(num - num2) < closest_dist):
                # Update the closest distance and indices
                closest_dist = abs(num - num2)
                
                # Determine the order of indices based on the magnitude of the numbers
                if num <= num2:
                    closest_inds = [ind, other_ind]
                else:
                    closest_inds = [other_ind, ind]
    
    # Return the indices of the closest pair
    return closest_inds

# Example 1
nums1 = [1, 7, 9, 2, 10]
print("List of numbers:", nums1)
print("Indices of the closest pair from the said list of numbers:")
print(test(nums1))

# Example 2
nums2 = [1.1, 4.25, 0.79, 1.0, 4.23]
print("\nList of numbers:", nums2)
print("Indices of the closest pair from the said list of numbers:")
print(test(nums2))

# Example 3
nums3 = [0.21, 11.3, 2.01, 8.0, 10.0, 3.0, 15.2]
print("\nList of numbers:", nums3)
print("Indices of the closest pair from the said list of numbers:")
print(test(nums3))

Sample Output:

List of numbers: [1, 7, 9, 2, 10]
Indices of the closest pair from the said list of numbers:
[0, 3]

List of numbers: [1.1, 4.25, 0.79, 1.0, 4.23]
Indices of the closest pair from the said list of numbers:
[4, 1]

List of numbers: [0.21, 11.3, 2.01, 8.0, 10.0, 3.0, 15.2]
Indices of the closest pair from the said list of numbers:
[2, 5]

Flowchart:

Flowchart: Python - Find the indices of the closest pair from given a list of numbers.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Circular shift number.
Next: Find a string which, when each character is shifted (ASCII incremented) by shift.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/puzzles/python-programming-puzzles-66.php