Answer to Question #337782 in Python for jamil

Question #337782

The coordinates of different persons are given in the following dictionary.

position = ('A': (2, 5), 'B': (1, 7), 'C': (9, 5), 'D': (2, 6))

You will take 2 inputs for person X's location where the first

input specifies person X's current location in the x-axis and the second input specifies person X's current location in the y-axis.

Your work is to find the person closest to person X by

calculating the

distance between person X and every other person in the dictionary.

Hint: The distance between two coordinates is given by the

formula (You

need to import math):

math.sqrt((x2-x1)**2 + (y2-y1)**2)

Sample Input 1:

Enter person X's x coordinate: 3

Enter person X's y coordinate: 6

Sample Output 1:

D



1
Expert's answer
2022-05-06T15:36:29-0400
position = {'A': (2, 5), 'B': (1, 7), 'C': (9, 5), 'D': (2, 6)}
x = int(input("Enter person X's x coordinate: "))
y = int(input("Enter person X's y coordinate: "))
min_dist = None
point_name = ''
for point in position:
	x1 = position[point][0]
	y1 = position[point][1]
	dist = (x-x1)**2 + (y-y1)**2
	if min_dist is not None:
		if min_dist > dist:
			min_dist = dist
			point_name = point
	else:
		min_dist = dist
		point_name = point
print(point_name)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS