Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 11 - Multidimensional Lists - Programming Exercises - Page 398: 11.50

Answer

code

Work Step by Step

# 11.50 (Geometry: find nearest points) When a new point is added to the plane, Listing # 11.5 finds the pair of two nearest points by examining the distance between every # pair of two points. This approach is correct, but not efficient. A more efficient algorithm # can be described as follows: # Let d be the current shortest distance between two # nearest points p1 and p2 # Let p be the new point added to the plane # For each existing point t: # if distance(p, t) < d: # d = distance(p, t) # p1, p2 = p, t # Rewrite Listing 11.5 using this new approach. def distance(x1, y1, x2, y2): return ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) ** 0.5 def nearestPoints(points): # p1 and p2 are the indices in the points list p1, p2 = 0, 1 # Initial two points shortestDistance = distance(points[p1][0], points[p1][1], points[p2][0], points[p2][1]) # Initialize shortestDistance # Compute distance for every two points for i in range(len(points)): for j in range(i + 1, len(points)): d = distance(points[i][0], points[i][1], points[j][0], points[j][1]) # Find distance if shortestDistance > d: p1, p2 = i, j # Update p1, p2 shortestDistance = d # New shortestDistance return p1, p2 def main(): numberOfPoints = eval(input("Enter the number of points: ")) # Create a list to store points points = [] print("Enter", numberOfPoints, "points:", end='') for i in range(numberOfPoints): point = 2 * [0] point[0], point[1] = \ eval(input("Enter coordinates separated by a comma: ")) points.append(point) # p1 and p2 are the indices in the points list p1, p2 = nearestPoints(points) # Display result print("The closest two points are (" + str(points[p1][0]) + ", " + str(points[p1][1]) + ") and (" + str(points[p2][0]) + ", " + str(points[p2][1]) + ")") main() # Call the main function
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.