Home > Blockchain >  Why isn't my output returning as expected?
Why isn't my output returning as expected?

Time:11-07

So I wrote this code

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 contains_word(grid: list[list[str]], w: str):
    if w in diagsUpRight(grid):
        return w
    if w in diagsDownRight(grid):
        return w
    if w in rows(grid):
        return w
    if w in cols(grid):
        return w

print(contains_word(grid=[
["r","a","w","b","i","t"],
["x","a","y","z","c","h"],
["p","q","b","e","i","e"],
["t","r","s","b","o","g"],
["u","w","x","v","i","t"],
["n","m","r","w","o","t"]]
, w='raw'))

For this code, I want to let contains_word return the word w if it is found in either rows(M), cols(M), diagsDownRight(M), diagsUpRight(grid)but when I put in the code as shown above, the output doesn't show up. What am I doing wrong here?

Edit I tried doing this and still the output returns None

def contains_word(grid: list[list[str]], w: str):
    for col in diagsUpRight(grid):
        if w in col:
            return w
    for col in diagsDownRight(grid):
        if w in col:
            return w
    for row in rows(grid):
        if w in row:
            return w
    for col in cols(grid):
        if w in col:
            return w

print(contains_word(grid=[["r","a","w","b","i","t"],
["x","a","y","z","c","h"],
["p","q","b","e","i","e"],
["t","r","s","b","o","g"],
["u","w","x","v","i","t"],
["n","m","r","w","o","t"]]
, w='raw'))

CodePudding user response:

It returns None then u have a list of lists, so you need to iterate over the grid in the contains_word function, currently you go over a list of lists and this list doenst resolve with your if in condition

CodePudding user response:

Your original rows(M) function returns a tuple (['rawbit', 'xayzch', 'pqbeie', 'trsbog', 'uwxvit', 'nmrwot'], ['tibwar', 'hczyax', 'eiebqp ',' gobsrt ',' tivxwu ',' towrmn ']). To fulfill the condition if w in rows(grid): it is necessary that w matches one of the lists of the tuple, for example, is the list ['rawbit', 'xayzch', 'pqbeie', 'trsbog', 'uwxvit ',' nmrwot ']. But since you are looking for the string raw this condition will never be met.

In particular, if you change the rows(M) function in this way, the result is raw

def rows(M):
    return ' '.join(''.join(l) for l in M)   ' '   ' '.join(''.join(reversed(l)) for l in M) # rawbit xayzch pqbeie trsbog uwxvit nmrwot tibwar hczyax eiebqp gobsrt tivxwu towrmn

contains_word returns:

raw

CodePudding user response:

Error:-

  • In your code every function returned tuples with 2 lists.
  • You iterated within the lists in your second code but the returned string is 'rawbit' so you need to iterate through 'rawbit' to get 'raw'.

Code:-

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)]
    return res

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 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


print(contains_word(grid=[["r","a","w","b","i","t"],
["x","a","y","z","c","h"],
["p","q","b","e","i","e"],
["t","r","s","b","o","g"],
["u","w","x","v","i","t"],
["n","m","r","w","o","t"]]
, w='raw'))
  • Related