我正在写一个功能路线。此函数具有必需的参数点,这些参数点采用点列表。如果依次访问给定列表中的每个点,则该函数必须返回行进的总距离。除强制参数外,该函数还有两个可选参数:
cycle:取一个布尔值,指示路径的末端是否等于其起点(True)或不是(False);此参数的默认值为False
距离:采用距离函数,用于计算给定路线中两个连续点之间的总距离;如果没有向此参数传递显式值,则必须使用欧几里德距离
问题:有人知道最后一个定义route()如何解决这个问题:
route([(41.79, 13.59), (41.68, 14.65), (21.16, -4.79)], distance=lambda p1, p2: abs(p1[0] + p2[0]))
正确答案:146.31
我所参考的部分代码:
if cycle == False and distance is λ(p1, p2): abs(p1[0] + p2[0]):
l = list()
count = 0
for items in range(len(points)-1):
a = points[items]
b = points[items+1]
d = euclidean(a[0], b[0])
l.append(d)
count += 1
return sum(l)
在这部分中,我陷入了第一条规则并且进一步。
完整的代码,工作正常(上述部分除外):
def euclidean(a, b):
'''
>>> euclidean((42.36, 56.78), (125.65, 236.47))
198.05484139500354
'''
from math import sqrt
return sqrt(sum((a - b)**2 for a, b in zip(a, b)))
def manhattan(c, d):
'''
>>> manhattan((42.36, 56.78), (125.65, 236.47))
262.98
'''
return sum(abs(c - d) for c, d in zip(c, d))
def chessboard(e, f):
'''
>>> chessboard((42.36, 56.78), (125.65, 236.47))
179.69
'''
return max(abs(e - f) for e, f in zip(e, f))
def route(points, cycle=False, distance=None):
'''
>>> route([(6.59, 6.73), (4.59, 5.54), (5.33, -13.98)])
21.861273201261746
>>> route(cycle=True, points=[(6.59, 6.73), (4.59, 5.54), (5.33, -13.98)])
42.60956710702662
>>> route([(6.59, 6.73), (4.59, 5.54), (5.33, -13.98)], distance=manhattan)
23.45
>>> route([(6.59, 6.73), (4.59, 5.54), (5.33, -13.98)], cycle=True, distance=manhattan)
45.42
'''
if cycle == False and distance is None:
l = list()
count = 0
for items in range(len(points)-1):
a = points[items]
b = points[items+1]
d = euclidean(a, b)
l.append(d)
count += 1
return sum(l)
if cycle == False and distance is euclidean:
l = list()
count = 0
for items in range(len(points)-1):
a = points[items]
b = points[items+1]
d = euclidean(a, b)
l.append(d)
count += 1
return sum(l)
if cycle == False and distance is λ(p1, p2): abs(p1[0] + p2[0]):
l = list()
count = 0
for items in range(len(points)-1):
a = points[items]
b = points[items+1]
d = euclidean(a[0], b[0])
l.append(d)
count += 1
return sum(l)
if cycle == True and distance is None:
l = list()
count = 0
for items in range(len(points)-1):
a = points[items]
b = points[items+1]
d = euclidean(a, b)
l.append(d)
count += 1
f = points[0]
g = points[-1]
r = euclidean(g, f)
k = sum(l) + r
return k
if cycle == True and distance is euclidean:
l = list()
count = 0
for items in range(len(points)-1):
a = points[items]
b = points[items+1]
d = euclidean(a, b)
l.append(d)
count += 1
f = points[0]
g = points[-1]
r = euclidean(g, f)
k = sum(l) + r
return k
if cycle is False and distance is manhattan:
l = list()
count = 0
for items in range(len(points)-1):
a = points[items]
b = points[items+1]
d = manhattan(a, b)
l.append(d)
count += 1
return sum(l)
if cycle is True and distance is manhattan:
l = list()
count = 0
for items in range(len(points)-1):
a = points[items]
b = points[items+1]
d = manhattan(a, b)
l.append(d)
count += 1
f = points[0]
g = points[-1]
r = manhattan(g, f)
k = sum(l) + r
return k
答案 0 :(得分:1)
我同意Duncan。你有太多的重复。 这是一个更直接的方法:
LocalDate myLocalDate = myResultSet.getObject( … , LocalDate.class ) ;
可以传递任何度量标准,然后使用它来代替欧几里德度量标准。