简体   繁体   中英

Recursive function returns "None" instead of requested list

I need to find all occurrences of a given string in a matrix, and return the position where the letters of each occurrence are. An example of the format of these inputs is:

soup = ["LAMXB","AOEYF","FCHTB","GFKAR","POSFD"]
text = "HOLA"

where text is a string and soup is a matrix of letters.

For this, I have the following code:

def valid_move(x, y, path, cant_row, cant_col):
    if (0<=x<=cant_row-1) and (0<=y<=cant_col-1) and ((x,y) not in path):
        return True
    else:
        return False

def busqueda(soup, text, row, col, path, index):
    soluciones = []
    cant_row = len(soup)
    cant_col = len(soup[0])

    if soup[row][col] != text[]:
        return

    if indice == len(text)-1:
        for pos_index in range(0,len(path)):
            soluciones.append(path[pos_index])
        soluciones.append((row,col))
        return soluciones

    path.append((row,col))

    for move in range(8):
        if valid_move(row+next_row[move],col+next_col[move],path,cant_row,cant_col):
            return busqueda(soup,text,row+next_row[move],col+next_col[move],path,index+1)

    path.pop()

def encontrar_ocurrencias(soup,text):
    cant_row = len(soup)
    cant_col = len(soup[0])

    next_row = [-1,-1,-1,0,0,1,1,1]
    next_col = [-1,0,1,-1,1,-1,0,1]
    path = []
    for i in range(0,cant_row):
        for j in range(0,cant_col):
            busqueda(soup,text,i,j,path,0)

''' The problem I have is that it's returning None instead of the list soluciones , which is the output I need.

In this particular case, the output I want is

[[(2,2),(1,1),(0,0),(0,1)],[(2,2),(1,1),(0,0),(1,0)]]

and if i do print(soluciones) instead of return soluciones i get the list above (so the code works) but if I use return , I get None

I've read similar questions asked in this page, but I still can't find the answer. I know I have to use return everytime I call a recursive function, but I can't still see where the mistake I've made is.

Thank you in advance for your help

There are a few typos in your code and both next_row and next_col needed to be available at a higher level - they weren't "known" in places they needed to be.

A great pass-through method for recursive solving is yield from (with yield at the point of solution). This is then a generator function so you will explicitly need to make a list if that is the required form of output:

next_row = [-1,-1,-1,0,0,1,1,1]
next_col = [-1,0,1,-1,1,-1,0,1]

def valid_move(x, y, path, cant_row, cant_col):
    return (x in range(cant_row)
            and y in range(cant_col)
            and (x,y) not in path )

def busqueda(soup, text, row, col, path, index):
    cant_row = len(soup)
    cant_col = len(soup[0])

    if soup[row][col] != text[index]:
        return

    path.append((row,col))
    if index == len(text)-1:
        yield (path.copy())
    else:
        for drow, dcol in zip(next_row,next_col):
            if valid_move(row+drow,col+dcol,path,cant_row,cant_col):
                yield from busqueda(soup,text,row+drow,col+dcol,path,index+1)
    path.pop()

def encontrar_ocurrencias(soup,text):
    cant_row = len(soup)
    cant_col = len(soup[0])

    path = []
    for i in range(0,cant_row):
        for j in range(0,cant_col):
            yield from busqueda(soup,text,i,j,path,0)

soup = ["LAMXB","AOEYF","FCHTB","GFKAR","POSFD"]
text = "HOLA"
result = list( encontrar_ocurrencias(soup,text) )
print(result)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM