Recursiveness in Python

Asked

Viewed 304 times

8

Next guys, I have a matrix that is a naval battle board represented by the list:

tab = [['.', '.', '#', '#', '#'], 
       ['.', '.', '.', '.', '.'], 
       ['#', '#', '#', '#', '#'], 
       ['.', '.', '.', '.', '.'], 
       ['#', '.', '#', '#', '.']]

where this recursion checks the '#' that are ship, and '#' next to '#' are big ship. Then the function runs through this list saves the positions of the large and small ships on the other lists.

Doubt: i would like to know the following, when that function that is below is executed it runs through a matrix, unfortunately it has to use the matrix itself, but when it finds x it returns the recursive function and the information previously is 'stacked' in the python compiler, but after it does this recursion it continues running stacking, i.e., it makes the n 2 loop after finishing the entire loop, it pops recursive function and back to loop n 2 which was what was happening earlier before it calls the recursive function, then I’d like to know if there’s a way after it with the recursive function it stop and do no more with n 2, if it was not very clear the question leaves a comment that I try to explain again with other words.

code:

def checar(c,l):
global navio
global  encontrados
global checado
if not checado:
    try:
        if tab[c][l] == '#':
            y = c
            x = l
            if y>=0 and x>=0:
                encontrados.append((y+1, x+1))
                checado.append((y, x))
    except IndexError:
        pass
try:
    if tab[c-1][l] == '#':
        y = c-1
        x = l
        if y>=0 and x>=0:
            tab[y][x] = 'x'
            encontrados.append((y+1,x+1))
            checado.append((y, x))
except IndexError:
    pass
try:
    if tab[c][l-1] == '#':
        y = c
        x = l-1
        if y>=0 and x>=0:
            tab[y][x] = 'x'
            encontrados.append((y+1,x+1))
            checado.append((y, x))
except IndexError:
    pass

try:
    if tab[c][l+1] == '#':
        y = c
        x = l+1
        if y>=0 and x>=0:
            tab[y][x] = 'x'
            encontrados.append((y+1,x+1))
            checado.append((y, x))
except IndexError:
    pass
try:
    if tab[c+1][l] == '#':
        y = c+1
        x = l
        if y>=0 and x>=0:
            tab[y][x] = 'x'
            encontrados.append((y+1,x+1))
            checado.append((y, x))
except IndexError:
    pass
tab[c][l] = '.'
for c in range(len(tab)):
   for l in range(len(tab)):
       if tab[c][l] == 'x':
           checar(c,l)
           tab[c][l] = '.'

inserir a descrição da imagem aqui

In this image from above is possible to visualize my doubt, I would like to know if it is possible after the end of the recursion 1, it does not pop, IE, it was not for the 2 and then 3 but it leaves the def and goes to the global direct. Or if there’s any better way to do that, I’m still a beginner in programming.

  • It probably doesn’t need to be recursive, but just one question: what should the end result be? What should they have checado, encontrado and navio?

  • The ships, the final result of found are the ships, whether it be small or large ship, ai after found it sends to the list ship that is this [[(1, 3),(1, 4), (1, 5)], [(3, 1), (3, 2), (3, 3), (3, 4), (3, 5)], [(5, 1)], [(5, 3), (5, 4)]] each list of this is a part of found that these are the locations of the ships, for when the user informs the entry of the bomb, I just go through the list with the information of the bomb and remove, in the end the amount left is the ships that have left, because to shoot down a ship, have to hit every part of it.

  • already the result of checked, is the same only that the list from 0 to n, already of found goes from 1 to n because the user puts the location of the pump informing line and column with numbers greater than 0.

  • and unfortunately I’m already seeing problems when it comes to checking, it’s all right to save the locations, but this check that will give me trouble

  • 2

    What is the expected output ? The ships would always be represented horizontally ? The odd lines of their matrix will always be formed by . (points) ? If a small ship is represented by a # (hashtag) and a big ship for two hashtags, what would be three hashtags ?

  • big ship also kkkk, it is a matter of Obi that is taking me out of the serious, 4 days I am working on it and all the ideas I have not right this is the last idea and just went wrong, it is extremely heavy this recursion, I have to deliver this Friday, kkkk if you are interested in the question is here the link: https://olimpiada.ic.unicamp.br/pratique/p2/2010/f1/batalha/

Show 1 more comment

1 answer

3


I don’t quite understand your question, but come on. Do you have a loop that calls a recursive functional and you want it to run this function once it does not run again? If this is it has several ways, put a flag that if it is true vc executes and if you have already executed vc puts it to false, the ultra option and put a break right after the lines you want to run only once so forcing the exit of the loop.

In the case of break:

    for c in range(len(tab)):
       for l in range(len(tab)):
           if tab[c][l] == 'x':
               checar(c,l)
               tab[c][l] = '.'
               break

In the case of Flag:

    flag = True
    for c in range(len(tab)):
       for l in range(len(tab)):
           if flag:
               if tab[c][l] == 'x':
                   checar(c,l)
                   tab[c][l] = '.'
                   flag = False

In the case of the flag and more interesting if you still want to go through the vector but without executing this block in specific, otherwise the best would be the break that will force the exit of the loop.

if you have syntax error and pq has been a while q not moving with Phyton.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.