起作用以我的代碼是找到最遠點的算法(可惜在O(N^2)),傳遞元組在Python
from math import sqrt
INT_MIN = -2147483648
def distance(a,b):
return sqrt((a[0]-b[0])**2 + (a[1]-b[1])**2)
def process(points):
# Function to produce tupple of two farthest Points
# currently O(N^2)
mx = INT_MIN
a,b = (0,0),(0,0)
# print points
for i in points:
for j in points:
if i == j:
continue
if distance(i,j) > mx:
mx = distance(i,j)
a = i,b = j
return (a,b)
L = [(66, 35), (67, 37), (67, 38), (68, 39)]
print(process(L))
然而,當運行該代碼產生以下錯誤:
Traceback (most recent call last):
File "./prog.py", line 22, in <module>
File "./prog.py", line 16, in process
File "./prog.py", line 4, in distance
TypeError: 'int' object is not subscriptable
任何想法如何將元組轉換爲'int'?
你試過把一個'打印(I,J)'在你內心的循環 - 都應該成爲明顯的... –
@喬恩克萊門特我試過了,但我仍然無法找到這樣的任務在邏輯上不正確!不要讓Pythonic的眼睛快速調試:p(開始python幾個月回來) – da6932