数独代码返回True或False

时间:2016-03-28 06:37:38

标签: python-2.7

如果给定输入(nxn方形数字列表)符合数独或不符合,则该过程需要返回True或False。给定列表应该返回True在任何行或列中没有数字出现多次

我的代码现在正在生成一个错误:

s[i][j] == s[i][j+1]:IndexError: list index out of range 

请建议如何解决上述问题以及任何其他问题以纠正此过程

def check_sudoku(s):        
    i = 0
    j = 0
    while i<len(s):
        while j<len(s):
            if s[i][j] == s[i][j+1]:
                return False
            j = j + 1
        i = i+1
    while j<len(s):
        while i<len(s):
            if s[i][j] == s[i+1][j]:
                return False
            i=i+1
        j=j+1
    return True

1 个答案:

答案 0 :(得分:0)

j应小于len(s)-1,因为当它达到len(s)-1时,j+1等于len(s)并提升IndexError

另外我认为j应该在len(s[i])-1而不是len(s)

的范围内
相关问题