@@ -0,0 +1,947 @@
+'''%0AWrite a function that takes the following inputs and gives the following outputs.%0A%0AInput: A list of points in 2-dimensional space, and an integer k%0AOutput: The k input points closest to (5, 5), using Euclidean distance%0A%0AExample:%0AInput: %7B(-2, -4), (0, 0), (10, 15), (5, 6), (7, 8), (-10, -30)%7D, k = 2%0AOutput: %7B(5, 6), (7, 8)%7D%0A'''%0A%0Aimport heapq%0A%0Adef closet_points(array, target, k):%0A    if not array or len(array) %3C k:%0A        return array%0A    n = len(array)%0A    heap = %5B%5D%0A    for point in array:%0A        dis = cal_distance(point, target)%0A        heap.append((dis, point))%0A    heapq.heapify(heap)%0A    result = %5B%5D%0A    for _ in xrange(k):%0A        result.append(heapq.heappop(heap)%5B1%5D)%0A        heapq.heapify(heap)%0A    return result%0A    %0Adef cal_distance(p, q):%0A    return (p%5B0%5D - q%5B0%5D)**2 + (p%5B1%5D - q%5B1%5D)**2%0A%0A%0Atest = %5B(-2, -4), (0, 0), (10, 15), (5, 6), (7, 8), (-10, -30)%5D%0Aprint closet_points(test, (5,5), 2)%0A~                                    %0A

Create K_closet_points.py