자주 나타나는 알고리즘들을 묶어서 알고리즘 패러다임이라고 부름
여러 알고리즘 패러다임으로 같은 문제 접근 가능
# 제곱근 사용을 위한 sqrt 함수
from math import sqrt
# 두 좌표 직선 거리를 계산해 주는 함수
def distance(store1, store2):
return sqrt((store1[0] - store2[0]) ** 2 + (store1[1] - store2[1]) ** 2)
# 가장 가까운 두 좌표 찾아주는 함수
def closest_pair(coordinates):
shortest = distance(coordinates[0], coordinates[1])
for i in range(len(coordinates)):
for j in range(i + 1, len(coordinates)):
dist = distance(coordinates[i], coordinates[j])
if dist < shortest:
shortest = dist
shortest_loc = [coordinates[i], coordinates[j]]
return shortest_loc
test_coordinates = [(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)]
print(closest_pair(test_coordinates))
# [(2, 3), (3, 4)]