Skip to content
Advertisement

word search in python :a search which return the indices the letters

#the following program checks the words in a 2D array and returns a nested list of the indices of the words.the algorithm checks the words in horizontal direction(left &right),vertical direction (upward and downward) and diagonally(left to right in the downward direction) **problem that i’am facing ** #the program returns correct indices for some of the words but for some it returns none.for some words the program crashes giving an index error(list index out of range) code

 def find(word):
   new_list=[]
   
   

   print("lenght",len(word))
   for i in range(3):
      for j in range(3):
         
        if board[i][j]==word[0] and board[i][j+1]==word[1] and board[i][j+2]==word[2]:#horizontal(right)
            
            x=i
            z=j
            
            
            for k in range(len(word)):
               #print('valuw of ',k)
               if board[x][z]==word[k]:
                  
                  
                  new_list.append(x)
                  new_list.append(z)
                  print(new_list)
                  z+=1
                  
                  if len(new_list) ==len(word)*2:
                     print(new_list) 
                     return new_list #returns nested list
        
        elif  board[i][j]==word[0] and board[i+1][j]==word[1] and board[i+2][j]==word[2]: vertical(downward)
            
            u=i
            v=j
            
            
            for k in range(len(word)):
            #print('valuw of ',k)
                if board[u][v]==word[k]:
                  
                  
                    new_list.append(u)
                    new_list.append(v)
                    print(new_list)
                    u+=1
                  
                    if len(new_list) ==len(word)*2:
                        print(new_list) 
                        return new_list
            
        elif board[i][j]==word[0] and board[i+1][j+1]==word[1] and board[i+2][j+2]==word[2]: #diagonal
            
            a=i
            b=j
            
            
            for k in range(len(word)):
               #print('valuw of ',k)
                if board[a][b]==word[k]:
                  
                  
                    new_list.append(a)
                    new_list.append(b)
                    print(new_list)
                    a+=1
                    b+=1
                    if len(new_list) ==len(word)*2:
                        print(new_list) 
                        return new_list                      
        elif board[i][j]==word[0] and board[i][j-1]==word[1] and board[i][j-2]==word[2]: #horizontal(left)
            
            c=i
            d=j
            
            
            for k in range(len(word)):
               #print('valuw of ',k)
                if board[c][d]==word[k]:
                  
                  
                    new_list.append(c)
                    new_list.append(d)
                    print(new_list)
                    
                    d-=1
                    if len(new_list) ==len(word)*2:
                        print(new_list) 
                        return new_list
        elif board[i][j]==word[0] and board[i-1][j]==word[1] and board[i-2][j]==word[2]: #verical(upward)
            
            f=i
            g=j
            
            
            for k in range(len(word)):
               #print('valuw of ',k)
                if board[c][d]==word[k]:
                  
                  
                    new_list.append(f)
                    new_list.append(g)
                    print(new_list)
                    
                    f-=1
                    if len(new_list) ==len(word)*2:
                        print(new_list) 
                        return new_list
board=[['p','o','t'],
        ['x','y','z'],
        ['a','p','o']]          
               
         

output=[]
l=['pot','pyo','ozt']
for word in l:
    
    output.append(find(word))
print(output)#print the nested list containing the indices enter code here


Advertisement

Answer

You need to check that the indices are actually valid for the array. If you have a 3×3 array and i=2,j=2, then j+1 will be off the board and Python will give you an IndexError. You have to be even more careful with negative indices as those will not give an error as board[-1][-1] is the same as board[2][2].

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement