Home > Software engineering >  Maximum recursion problem on Python, problem with a recursive function
Maximum recursion problem on Python, problem with a recursive function

Time:05-25

import sys
import random
import numpy as np
import matplotlib.pyplot as plt
sys.setrecursionlimit(10**7)

nL,nC=100,100
a,b=50,50
c = 5
n=0
L=np.zeros((nL,nC))
T = []
def case_prox(L1:list,i,j):   
    l=len(L1)
    return [[L1[(i 1)%l][j],(i 1)%l,j],[L1[(i-1)%l][j],(i-1)%l,j],[L1[i][(j 1)%l],i,(j 1)%l],[L1[i][(j-1)%l],i,(j-1)%l]]

def avancer(L,i,j):
    global n,S,T,p
    S = []
    p = 0
    while n<150:#global dans le while
        a = random.randrange(0,len(case_prox(L,i,j)))
        
        if L[case_prox(L,i,j)[a][1]][case_prox(L,i,j)[a][2]] == 0:
            L[case_prox(L,i,j)[a][1]][case_prox(L,i,j)[a][2]] = 1
            n =1
            avancer(L,50,50)
            S.append(p 1)
        else:
            avancer(L,case_prox(L,i,j)[a][1],case_prox(L,i,j)[a][2])
            n =1
            p =1
    print(L)
                
                

L[50][50]=1

print(case_prox(L,30,30))

print(case_prox(L,30,30)[0][1],case_prox(L,30,30)[0][2])

avancer(L,50,50)
plt.plot(S)

I made an algorithm based on Internal Diffusion Limited Aggregation. The principal function is recursive so I have to call the function a lot of time. I decide to increase the recursion limitnevertheless the problem is not solved. The function works for a long time and do a role. Please Can you help me?

CodePudding user response:

I think you want to iterate n prior to calling avancer in the else, otherwise you risk an infinite loop...

def avancer(L,i,j):
    global n,S,T,p
    S = []
    p = 0
    while n<150:#global dans le while
        a = random.randrange(0,len(case_prox(L,i,j)))
        
        if L[case_prox(L,i,j)[a][1]][case_prox(L,i,j)[a][2]] == 0:
            L[case_prox(L,i,j)[a][1]][case_prox(L,i,j)[a][2]] = 1
            n =1
            avancer(L,50,50)
            S.append(p 1)
        else:
            n =1
            avancer(L,case_prox(L,i,j)[a][1],case_prox(L,i,j)[a][2])
            p =1
    print(L)
  • Related