如何尝试:除了:不无限地工作

2024-05-06 01:34:42 发布

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

所以我有一个2d列表,名为tour:

tour = [ [3, 2, 1,], 
         [4, 7, 9,], 
         [5, 6, 8,], ]

我想让我的代码检查这个列表,看看一个国王(国际象棋)是否能正常通过它。 我所做的代码可以应用于N×N 2d列表。它的作用是从数字1开始,检查它周围的所有9个空格是否有数字2,重复这个过程直到N×N。问题是,当数字在列表的一侧时,代码会给出一个索引器,为了克服这个问题,我做了一个try: except: IndexError,但现在当它到达i = 6时,它会无限继续运行

def in_list(number, listname):
    """
    this function detects where a certain number is in a 2d list and returns the index of 
    it
    """
    for i, sublist in enumerate(listname):
        if (number in sublist):
            return [i, sublist.index(number)]
        
    return -1
            

def is_king_tour_3by3(tour):
    """
    this function checks for all 9 areas around this number and if it's an IndexError
    i.e. it's at the end of the list it ignores that case
    """
    start = in_list(1, tour)
    i = 2
    counter = len(tour) * len(tour)
    
    while True:
        counter -= 1
        print(i)
        print(start)
        try:
            if(tour[start[0]][start[1] - 1] == i): #starting from checking the number on the left and going counter-clockwise
                i += 1
                start[1] -= 1
            elif(tour[start[0] + 1][start[1] - 1] == i):    
                i += 1
                start[0] += 1
                start[1] -= 1
            elif(tour[start[0] + 1][start[1]] == i):
                i += 1
                start[0] += 1
            elif(tour[start[0] + 1][start[1] + 1] == i):
                i += 1
                start[0] += 1
                start[1] += 1
            elif(tour[start[0]][start[1] + 1] == i):
                i += 1
                start[1] += 1
            elif(tour[start[0] - 1][start[1] + 1] == i):
                i += 1
                start[0] -= 1
                start[1] += 1
            elif(tour[start[0] - 1][start[1]] == i):
                i += 1
                start[0] -= 1
            elif(tour[start[0] - 1][start[1] - 1] == i):
                i += 1
                start[0] -= 1
                start[1] -= 1
        except IndexError:
            pass
        
        if(i == (len(tour) * len(tour))):
            return True
            break
        elif(counter == 0):
            return False
            break

 

Tags: the代码innumber列表lenreturnif
1条回答
网友
1楼 · 发布于 2024-05-06 01:34:42

通过将start[0] < len(tour)-1 and start[1] > 0添加到行中,可以检查您是否在2d列表之外,而不是使用try: except:。只要start为>;则此代码将解决此问题;0及<len(tour)(如果start[0]或start[1]为-1或len(tour),则可能会失败):

def in_list(number, listname):
    """
    this function detects where a certain number is in a 2d list and returns the index of 
    it
    """
    for i, sublist in enumerate(listname):
        if (number in sublist):
            return [i, sublist.index(number)]
    
return -1
        

def is_king_tour_3by3(tour):
    """
    this function checks for all 9 areas around this number and if it's an IndexError
    i.e. it's at the end of the list it ignores that case
    """
    start = in_list(1, tour)
    i = 2
    counter = len(tour) * len(tour)
    
while True:
    counter -= 1
    print(i)
    print(start)
    if(start[1] > 0 and tour[start[0]][start[1] - 1] == i): #starting from checking the number on the left and going counter-clockwise
        i += 1
        start[1] -= 1
    elif(start[0] < len(tour)-1 and start[1] > 0 and tour[start[0] + 1][start[1] - 1] == i):    
        i += 1
        start[0] += 1
        start[1] -= 1
    elif(start[0] < len(tour)-1 and tour[start[0] + 1][start[1]] == i):
            i += 1
            start[0] += 1
    elif(start[0] < len(tour)-1 and start[1] < len(tour)-1 and tour[start[0] + 1][start[1] + 1] == i):
        i += 1
        start[0] += 1
        start[1] += 1
    elif(start[1] < len(tour)-1 and tour[start[0]][start[1] + 1] == i):
        i += 1
        start[1] += 1
    elif(start[0] > 0 and start[1] < len(tour)-1 and tour[start[0] - 1][start[1] + 1] == i):
        i += 1
        start[0] -= 1
        start[1] += 1
    elif(start[0] > 0 and tour[start[0] - 1][start[1]] == i):
        i += 1
        start[0] -= 1
    elif(start[0] > 0 and start[1] > 0 and tour[start[0] - 1][start[1] - 1] == i):
        i += 1
        start[0] -= 1
        start[1] -= 1
    
    if(i == (len(tour) * len(tour))):
        return True
        break
    elif(counter == 0):
        return False
        break

tour = [ [3, 2, 1,], 
         [4, 7, 9,], 
         [5, 6, 8,], ]
is_king_tour_3by3(tour)

相关问题 更多 >