-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedIn_find_nearest_k_points_from_central_point.py
68 lines (56 loc) · 2.59 KB
/
linkedIn_find_nearest_k_points_from_central_point.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#This problem was asked by LinkedIn.[Hard]
#Given a list of points, a central point, and an integer k, find the nearest k points from the central point.
# For example, given the list of points [(0, 0), (5, 4), (3, 1)], the central point (1, 2), and k = 2, return [(0, 0), (3, 1)].
#Solution using itemgetter():
# operator.itemgetter(n) constructs a callable that assumes an iterable object (e.g. list, tuple, set) as input, and fetches the n-th element out of it.
# The key= parameter of sort requires a key function (to be applied to be objects to be sorted) rather than a single key value and
# that is just what operator.itemgetter(1) will give you: A function that grabs the first item from a list-like object.
from operator import itemgetter
def kNearestPoints(points, central_point, k):
# input: list of points (x, y), a central point (x_C, y_c) & int k
# output: list of k nearest points from central point
# trival cases
if k <= 0:
return []
if k >= len(points):
return points
# else
x_c = central_point[0] # x-coordinate of central point
y_c = central_point[1] # y-coordinate of central point
distance_list = [] # list of [point (x,y), distance to central point] initialized
kList = [] # list of k nearest points initialized
# compute distance from each points to central points, then push entry into distance_list
for point in points:
x = point[0]
y = point[1]
d = (x - x_c)**2 + (y - y_c)**2 # distance ^ 2
distance_list.append([point, d])
distance_list = sorted(distance_list, key=itemgetter(1)) # sort distance_list ascendingly by distance
# add k nearest points into kList
for i in range(k):
chosenPoint = distance_list[i][0]
kList.append(chosenPoint)
return kList
# Tests
assert kNearestPoints(
[(0, 0), (5, 4), (3, 1)],(1, 2), 2) == [(0, 0), (3, 1)]
#Solution using sqrt() :
# here targets = list of points(x,y) , source = central points(x_C, y_c)
import math
# compute distance from each points to central points using sqrt()
def compute_distance(source, target):
return math.sqrt(
(source[0] - target[0]) ** 2 +
(source[1] - target[1]) ** 2
)
def kNearestPoints(targets, source, k):
if k >= len(targets):
return targets
# sort targets by calculate_distance and return nearest_points
nearest_points = \
sorted(targets, key=lambda x: compute_distance(source, x))[:k]
return nearest_points
# Tests
assert compute_distance((0, 0), (3, 4)) == 5
assert kNearestPoints(
[(0, 0), (5, 4), (3, 1)],(1, 2), 2) == [(0, 0), (3, 1)]