Home > Enterprise >  How can I let my code return every string that is available in the grid?
How can I let my code return every string that is available in the grid?

Time:11-10

So I learnt how to write this code with the help of (https://stackoverflow.com/users/5237560/alain-t) to return all the elements in the list that appear in grid: list and here it is.

def diagsDownRight(M):
    n = len(M)
    m = [[''] * (n - i - 1)   row   [''] * i for i, row in enumerate(M)]
    return [''.join(col) for col in zip(*m)] [''.join(col[::-1]) for col in zip(*m)]

def diagsUpRight(M):
    n = len(M)
    m = [['']*i   row   ['']*(n-i-1) for i, row in enumerate(M)]
    return [''.join(col) for col in zip(*m)] [''.join(col[::-1]) for col in zip(*m)]

def rows(M):
    return ["".join(row) for row in M] ["".join(reversed(row)) for row in M]

def cols(M):
    return ["".join(col) for col in zip(*M)]  [''.join(col[::-1]) for col in zip(*M)]

def sleuth(grid: list[list[str]], words: list):
    l = []
    for word in words:
        if word in diagsUpRight(grid):
            l.append(word)
        if word in diagsDownRight(grid):
            l.append(word)
        if word in rows(grid):
            l.append(word)
        if word in cols(grid):
            l.append(word)
     return l

However, when I run sleuth(grid, words) the output returns None. What am I doing wrong here?

btw grid is the grid of the puzzle

and words are the list from where we have to find whether each word occurs in grid or not.

For the ones that appear, I want to return the word

Edit the expected input is ["bog", "moon", "rabbit", "the", "bit", "raw"] for

grid = 
["r","a","w","b","i","t"],
["x","a","y","z","c","h"],
["p","a","b","e","i","e"],
["t","r","s","b","o","g"],
["u","t","x","v","i","t"],
["n","m","r","w","o","t"]]

and the output should be

["raw", "bit","rabbit", "bog", "the"]

CodePudding user response:

rows() returns this list of words:

['rawbit', 'xayzch', 'pabeie', 'trsbog', 'utxvit', 'nmrwot', 'tibwar', 'hczyax', 'eiebap', 'gobsrt', 'tivxtu', 'towrmn']

The exact string bog is not present in that list.

You need to check for sub-strings also.

CodePudding user response:

Code:-

def sleuth(grid: list[list[str]], list_of_words: list):
    new_list_of_words=[]
    for w in list_of_words:
        for words in diagsUpRight(grid):
            if w in words:
                new_list_of_words.append(w)
        for words in diagsDownRight(grid):
            if w in words:
                new_list_of_words.append(w)
        for words in rows(grid):
            if w in words:
                new_list_of_words.append(w)
        for words in cols(grid):
            if w in words:
                new_list_of_words.append(w)
    return new_list_of_words
print(sleuth(            
[["r","a","w","b","i","t"],
 ["x","a","y","z","c","h"],
 ["p","a","b","e","i","e"],
 ["t","r","s","b","o","g"],
 ["u","t","x","v","i","t"],
 ["n","m","r","w","o","t"]],
 ["bog", "moon", "rabbit", "the", "bit", "raw"]
))

Output:-

['bog', 'rabbit', 'the', 'bit', 'bit', 'raw']

Next code:-

def contains_word(grid: list[list[str]], w: str):
    for words in diagsUpRight(grid):
        if w in words:
            return w
    for words in diagsDownRight(grid):
        if w in words:
            return w
    for words in rows(grid):
        if w in words:
            return w
    for words in cols(grid):
        if w in words:
            return w
grid=[["r","a","w","b","i","t"],
 ["x","a","y","z","c","h"],
 ["p","a","b","e","i","e"],
 ["t","r","s","b","o","g"],
 ["u","t","x","v","i","t"],
 ["n","m","r","w","o","t"]]
print([contains_word(grid,x) for x in ["bog", "moon", "rabbit", "the", "bit", "raw"] if contains_word(grid,x)!=None])

Output:-

['bog', 'rabbit', 'the', 'bit', 'raw']
PS:- https://stackoverflow.com/a/69871284/16321225 -- Just a bit of logic in my previous answer would work.
  • Related