猿问

返回列表中的最低点

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

给出一个列表或点。我想返回下一个最低点。我试图弄清楚如何做到这一点。许多文档字符串是测试用例,以显示所需的意图。问题是它不返回下一个最小的项目,而是返回整体最小的项目。所有它不返回索引,而只返回值


鸿蒙传说
浏览 111回答 1
1回答

婷婷同学_

您的代码不使用该start参数,这就是它打印总体最小值的原因。更改for-loops 中的范围以使用start的元素作为起始值。此外,lowest_point如果您需要该值,该函数应该返回;如果您需要索引使用额外的变量来跟踪它们:def find_local_sink(m, start):&nbsp; &nbsp; lowest_point = m[start[0]][start[1]]&nbsp; &nbsp; lowest_index = [[start[0], start[1]]&nbsp; &nbsp; for i in range(start[0], len(m)):&nbsp; &nbsp; &nbsp; &nbsp; for j in range(start[1], len(m)):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if m[i][j] < lowest_point:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lowest_point = m[i][j]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lowest_index = [i, j]&nbsp; &nbsp; return lowest_index # or: return lowest_point
随时随地看视频慕课网APP

相关分类

Python
我要回答