[Python] Keep getting runtime error TypeError: cannot concatenate 'str' and 'list'. Works fine on my machine.


  • 2
    S

    I keep getting TypeError: cannot concatenate 'str' and 'list' objects on python. This code works fine on my local machine. The error is on line cell1 += board[i+j][:3]. This is very frustrating. Could anyone enlighten me why this works on my local machine but not here?

    class Solution:
            # @param board, a 9x9 2D array
            # @return a boolean
            def isValidSudoku(self, board):
                #check row
                for row in board:
                    if not isValid(row):
                        return False
                
                #check column
                for c in range(9):
                    col = ''
                    for r in range(9):
                        col += board[r][c]
                    if not isValid(col):
                        return False
                
                #check cell
                for i in [0,3,6]:
                    cell1 = ''
                    cell2 = ''
                    cell3 = ''
                    for j in [0,1,2]:
                        cell1 += board[i+j][:3]
                        cell2 += board[i+j][3:6]
                        cell3 += board[i+j][6:]
                        
                    if isValid(cell1) and isValid(cell2) and isValid(cell3):
                        pass
                    else:
                        return False
                    
                return True    
            
        def isValid(s):
            # check if this array makes sense
            A = [0]*10
            #print s
            for char in s:
                if char != '.':
                    num = int(char)
                    if A[num] != 0:
                        return False
                    else:
                        A[num] = 1
            
            return True

  • 1
    S

    This is a total bullshit. I cannot believe I've wasted several hours resolving this. I've rephased my code (without changing the behavior) and it's now accepted.
    Obviously the Python online judge has a ridiculous bug which is activated when a empty string '' is appended with += operator. See the code below.

    class Solution:
        # @param board, a 9x9 2D array
        # @return a boolean
        def isValidSudoku(self, board):
            
            #check row
            for row in board:
                if not isValid(row):
                    return False
            
            #check column
            for c in range(9):
                col = ''
                for r in range(9):
                    col += board[r][c]
                if not isValid(col):
                    return False
                    
            #check cell
            for r in [0,3,6]:
                cell1 = board[r][:3]  + board[r+1][:3]  + board[r+2][:3] 
                cell2 = board[r][3:6] + board[r+1][3:6] + board[r+2][3:6]
                cell3 = board[r][6:]  + board[r+1][6:]  + board[r+2][6:]
                if isValid(cell1) and isValid(cell2) and isValid(cell3):
                  pass
                else:
                  return False
                
            return True    
        
    def isValid(s):
        # check if this array makes sense
        A = [0]*10
        #print s
        for char in s:
            if char != '.':
                num = int(char)
                if A[num] != 0:
                    return False
                else:
                    A[num] = 1
        
        return True

  • 0
    Q

    I also encountered this problem in python. Thanks for your solution sharing


  • 0
    S

    Please up vote the main question so people can see this, save time.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.