nearest neighbour algorithm as a coding question
Anonymous
def find_k_nearest_stores(stores, user_location, k): distances = {} for store in stores: distances[store["store_id"]]=math.sqrt((user_location[0]-store["lat"])**2 +(user_location[1]- store["lon"])**2) heapq.heappush() sorted_distances= sorted(distances.items(),key=lambda x: x[1], reverse=False) nearest_stores = [store_id for store_id, _ in sorted_distances[:k]] return nearest_stores
Check out your Company Bowl for anonymous work chats.