Search code examples
pythonrecursionmatrixmatching

Match a word's letters to existing letters in a matrix


I'm making a crossword style program with python using recursive functions. First one function takes random words from a list and places them vertically in a square matrix (can be any length) with '-' as default items. The second function does the same but places the words horizontally after matching the words' letters to those of the vertical words already placed. For some reason, the letter matching isn't working, and the function is replacing the existing vertical words' letters with the new words' horizontal letters.

The vertical function looks like this and works fine:

def placeWordsVertically(puzzle, row, column): ##puzzle is the matrix, created in earlier helper function 
    if column >= len(puzzle):
        return puzzle
    else:
        word = getAWord() ## helper function defined elsewhere which chooses a random word from a list
        
        if len(word)<=len(puzzle):
            for i in range(len(word)):
                puzzle[i][column] = word[i]   
            column += 2
            placeWordsVertically(puzzle, row, column)
        else:
            placeWordsVertically(puzzle, row, column)

Here is the horizontal function:

def placeWordsHorizontally(puzzle, row, column):
    if row >= len(puzzle):
        return puzzle

    else:
        word = getAWord()

        ## set length of word as variable 1
        l = len(word)
        
        #check if word fits puzzle length
        if l <= len(puzzle):

            ##make sure there's a space ('-') at the end of horizontal words
            if puzzle[row][l] == '-':

                ##BELOW IS THE PART NOT WORKING
                for i in range(len(word)):
                    ## existing letters in the row should either match the new word's letters or be '-'
                    if puzzle[row][i] == word[i] or '-':
                        puzzle[row][i] = word[i]
                    else:  
                        placeWordsHorizontally(puzzle, row, column)
                # increase row by 2
                row += 2
                placeWordsHorizontally(puzzle, row, column)
            else:
                placeWordsHorizontally(puzzle, row, column) 
        else:
            placeWordsHorizontally(puzzle, row, column)

This is the result I got from running the program:

t r a v e l e r s - u - 
i - i - i - i - i - r - 
s t o w i n g - r - g - 
i - d - e - h - w - e - 
e t h a n o l - o - - - 
a - i - s - r - r - - - 
s o m n a m b u l a r - 
i - g - - - n - h - - - 
b i r d e r s - y - - - 
m - - - - - - - - - - - 
d e s i r e s - - - - - 
- - - - - - - - - - - - 

The vertical words are messed up because they got overwritten. Anyone have any idea how to match the horizontal words' letters to the existing vertical words' letters?


Solution

  • The problem is the puzzle[row][i] == word[i] or '-': condition. This will always return True.

    The reason is that the or operator does not repeat the left side value of the previous comparison, it uses the "Truth value" of the character '-'.

    For it to work, you need to write the condition like this:

    if puzzle[row][i] == word[i] or puzzle[row][i] == '-':
    

    otherwise Python treats it as:

    if (puzzle[row][i] == word[i]) or bool('-'):
    
    bool('-') # is True