새로새록
두가지 방법 - 좌표 사이 최단거리 '좌표쌍 반환' 본문
210520
# 제곱근 사용을 위한 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):
min = distance(coordinates[0], coordinates[1])
min_pair = [coordinates[0], coordinates[1]]
for i in coordinates:
for j in coordinates:
if distance(i, j) < min and distance(i, j) != 0:
min = distance(i, j)
min_pair = [i, j]
return min_pair
###
# 테스트
def closest_pair(coordinates):
pair = [coordinates[0], coordinates[1]]
for i in range(len(coordinates)-1):
for j in range(i+1, len(coordinates)):
store1, store2 = coordinates[i], coordinates[j]
if distance(pair[0], pair[1]) >distance(store1, store2):
pair = [store1, store2]
return pair
'소프트웨어융합 > 코드잇 정리.py' 카테고리의 다른 글
lambda + greedy algorithm 리스트 수강신청 (0) | 2021.06.01 |
---|---|
초코파이 장사 dynamic (2) | 2021.05.31 |
memoization vs tabulation : 피보나치수열 (1) | 2021.05.30 |
divide&conquer -> 퀵정렬 quick_sort -> partition 함수 구현하기 (0) | 2021.05.24 |
리스트 조건(홀수/짝수) 제거했는데 하나(두번째 원소)가 덜 제거된 이유 (1) | 2021.05.15 |