Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- 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}]
- P = {'x': 1,'y': -1}
- def distanceCoordMap(S,P):
- INSTANCES = []
- for v in S:
- coord = {}
- x = int(v['x'])
- y = int(v['y'])
- distance = math.sqrt(((P['x'] - x) **2) + ((P['y'] - y) ** 2))
- coord['x'] = x
- coord['y'] = y
- coord['distance'] = distance
- INSTANCES.append(coord)
- return INSTANCES
- def collectSort(INSTANCES):
- DISTANCES = []
- for d in INSTANCES:
- DISTANCES.append(d['distance'])
- DISTANCES.sort()
- return DISTANCES
- def nearestCoordPair(S,P):
- INSTANCES = distanceCoordMap(S,P)
- DISTANCES = collectSort(INSTANCES)
- for i in INSTANCES:
- if i['distance'] == DISTANCES[0]:
- return "(%s,%s) nearest (%s,%s) with distance of %s" % (i['x'],i['y'],P['x'],P['y'],i['distance'])
- print nearestCoordPair(S,P)
Add Comment
Please, Sign In to add comment