Guest User

Untitled

a guest
Jan 23rd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. import math
  2.  
  3. S = [{'x': 2,'y': 7},{'x': 10,'y': 5},{'x': 8,'y': -2},{'x': 7,'y': -6},{'x': -3,'y': -5},{'x': -8,'y': 0},{'x': -5,'y': 6}]
  4. P = {'x': 1,'y': -1}
  5.  
  6. def distanceCoordMap(S,P):
  7. INSTANCES = []
  8. for v in S:
  9. coord = {}
  10. x = int(v['x'])
  11. y = int(v['y'])
  12. distance = math.sqrt(((P['x'] - x) **2) + ((P['y'] - y) ** 2))
  13. coord['x'] = x
  14. coord['y'] = y
  15. coord['distance'] = distance
  16. INSTANCES.append(coord)
  17. return INSTANCES
  18.  
  19. def collectSort(INSTANCES):
  20. DISTANCES = []
  21. for d in INSTANCES:
  22. DISTANCES.append(d['distance'])
  23. DISTANCES.sort()
  24. return DISTANCES
  25.  
  26. def nearestCoordPair(S,P):
  27. INSTANCES = distanceCoordMap(S,P)
  28. DISTANCES = collectSort(INSTANCES)
  29. for i in INSTANCES:
  30. if i['distance'] == DISTANCES[0]:
  31. return "(%s,%s) nearest (%s,%s) with distance of %s" % (i['x'],i['y'],P['x'],P['y'],i['distance'])
  32.  
  33. print nearestCoordPair(S,P)
Add Comment
Please, Sign In to add comment