def find_local_sink(m: List[List[int]], start: List[int]) -> List[int]: """ Examples >>> m = [[ 5,70,71,80], [50, 4,30,90], [60, 3,35,95], [10,72, 2, 1]] >>> find_local_sink(m, [0,0]) [3,3] >>> m = [[ 5,70,71,80], [50, 4, 5,90], [60, 3,35, 2], [ 1,72, 6, 3]] >>> find_local_sink(m, [0,3]) [2,3] >>> m = [[9,2,3], [6,1,7], [5,4,8]] >>> find_local_sink(m, [1,1]) [1,1] """ lowest_point = m[0][0] for i in range(len(m)): for j in range(len(m)): if m[i][j] < lowest_point: lowest_point = m[i][j] print(lowest_point) return find_local_sink给出一个列表或点。我想返回下一个最低点。我试图弄清楚如何做到这一点。许多文档字符串是测试用例,以显示所需的意图。问题是它不返回下一个最小的项目,而是返回整体最小的项目。所有它不返回索引,而只返回值
1 回答

婷婷同学_
TA贡献1844条经验 获得超8个赞
您的代码不使用该start参数,这就是它打印总体最小值的原因。更改for-loops 中的范围以使用start的元素作为起始值。此外,lowest_point如果您需要该值,该函数应该返回;如果您需要索引使用额外的变量来跟踪它们:
def find_local_sink(m, start):
lowest_point = m[start[0]][start[1]]
lowest_index = [[start[0], start[1]]
for i in range(start[0], len(m)):
for j in range(start[1], len(m)):
if m[i][j] < lowest_point:
lowest_point = m[i][j]
lowest_index = [i, j]
return lowest_index # or: return lowest_point
添加回答
举报
0/150
提交
取消