Reputation: 3
I don't know how can I apply Big-O notation to distance problem code. Could you advise me?
from math import sqrt
a = [4, 2, 8, 5, 6, 2, 6]
b = [3, 6, 4, 7, 4, 3, 9]
choose = int(input("1.맨하탄 2.유클리드 번호를 입력하시오 "))
def euclidean_distance(x,y):
return sqrt(sum(pow(q-p,2) for p,q in zip(x,y)))
def manhattan_distance(x,y):
return sum(abs(p-q) for p,q in zip(x,y))
if 0 < choose < 2 :
print(manhattan_distance(a,b))
elif 1 < choose < 3 :
print(euclidean_distance(a,b))
else:
print("Please input 1 or 2")
Upvotes: 0
Views: 40
Reputation: 23508
The complexity of your program is one simple loop over the array.
If you denote the array size as N
, the complexity would be O(N)
.
Upvotes: 2