(遍历)如何在python中解决这个问题?

2024-09-30 12:34:05 发布

您现在位置:Python中文网/ 问答频道 /正文

  • 编写一个函数traverse(),该函数接收n个字符串的列表,每个字符串包含n个小写字符 (a-z)
  • tb表示一个包含n行和n列的正方形表格。该函数返回由下面的过程生成的字符串st,该字符串从左上角单元格开始遍历网格,并在右下角单元格结束
  • 在每一步中,程序都会水平向右或垂直向下移动,这取决于两个单元格中哪个单元格有一个较小的字母(即按字母顺序较早出现的字母)
  • 然后将访问单元格中的字母添加到st。如果出现连接,则可以选择任意方向
  • 当到达表的右边缘或下边缘时,显然只有一个唯一的下一个单元格可移动。例如,traverse(["veus", "oxde", "oxlx", "hwuj"])返回"veudexj"

因此,该表将如下所示:

v  o  o  h
e  x  x  w
u  d  l  u
s  e  x  j

我是python新手,我写了这段代码……但它只打印"veuexj"我想说问题出在这一行if new_list[a - 1][b - 1] == new_list[a - 1][-2]:,这迫使参数跳过'd'字符。我不知道如何解决它

def traverse(tb_list):
    new_list = tb_list.copy()
    st = new_list[0][0]
    parameter = ''
    handler = 1
    for a in range(1, len(new_list)):
        
        for b in range(handler, len(new_list[a])):

            if new_list[a - 1][b - 1] == new_list[a - 1][-2]:
                parameter = new_list[a][b]

            elif new_list[a - 1][b - 1] > min(new_list[a - 1][b], new_list[a][b - 1]):
                parameter = min(new_list[a - 1][b], new_list[a][b - 1])

            elif new_list[a - 1][b - 1] < min(new_list[a - 1][b], new_list[a][b - 1]):
                parameter = min(new_list[a - 1][b], new_list[a][b - 1])

            st += parameter

        handler = b

    return st


print(traverse(["veus", "oxde", "oxlx", "hwuj"]))

Tags: 函数字符串newparameter字母min字符tb
1条回答
网友
1楼 · 发布于 2024-09-30 12:34:05

您可以尝试这样的方法(解释添加为注释):

def traverse(tb_list):
    lst = tb_list.copy() #Takes a copy of tb_list
    lst = list(zip(*[list(elem) for elem in lst])) #Transposes the list
    final = lst[0][0] #Sets final as the first element of the list
    index = [0,0] #Sets index to 0,0

    while True:
        x = index[0] #The x coordinate is the first element of the list
        y = index[1] #The y coordinate is the second element of the list

        if x == len(lst) - 1: #Checks if the program has reached the right most point of the table
            if y == len(list(zip(*lst))) - 1: #Checks if program has reached the bottommost point of the table
                return final #If both the conditions are True, it returns final (the final string)
            else:
                final += lst[x][y+1] #If the program has reached the right most corner, but not the bottommost, then the program moves one step down
                index = [x, y+1] #Sets the index to the new coordinates

        elif y == len(list(zip(*lst))) - 1: #Does the same thing in the previous if condition button in an opposite way (checks if program has reached bottommost corner first, rightmost corner next)
            if x == len(lst) - 1:
                return final
            else:
                final += lst[x + 1][y] #If the program has reached the bottommost corner, but not the rightmost, then the program moves one step right
                index = [x + 1, y]

        else: #If both conditions are false (rightmost and bottommost)
            if lst[x+1][y] < lst[x][y+1]: #Checks if right value is lesser than the bottom value
                final += lst[x+1][y] #If True, then it moves one step right and adds the alphabet at that index to final
                index = [x+1,y] #Sets the index to the new coords
            else: #If the previous if condition is False (bottom val > right val)
                final += lst[x][y+1] #Moves one step down and adds the alphabet at that index to final
                index = [x,y+1] #Sets the index to the new coords

lst = ["veus", "oxde", "oxlx", "hwuj"]
print(traverse(lst))

输出:

veudexj

我已将解释添加为注释,因此请您慢慢看一看。如果您仍然不清楚代码的任何部分,请随时询问我。欢迎提出任何优化/缩短我的代码的建议

相关问题 更多 >

    热门问题