Good, I have a doubt, I'm doing unittest for a function that I have and the problem is that when generating a 40x40 matrix, 50x50 of the same element (for example 'A') onwards generates a recursion error, while that with a function of 1000x1000 random of A..Z does not generate any error to me.
The code is as follows.
Adjacent calculation function:
def calcular(matriz):
matriz = [list(row) for row in matriz]
m = len(matriz) # filas
n = len(matriz[0]) # columnas
d = {} # diccionario de manchas
def busqueda(f, c, value, id):
vecinos = ((i, j) for i, j in ((f - 1, c), (f, c - 1), (f, c + 1), (f + 1, c))
if i >= 0 and i < m and j >= 0 and j < n and matriz[i][j] == value)
for i, j in vecinos:
d[id].append((value, i, j))
matriz[i][j] = False
busqueda(i, j, value, id)
for i in range(m):
for j in range(n):
if matriz[i][j]:
id = matriz[i][j], i, j
d[matriz[i][j], i, j] = []
busqueda(i, j, matriz[i][j], id)
max = 0
kmax = 0
for key, value in d.items():
if len(value) > max:
max = len(value)
kmax = key[0]
resultado = (kmax + ',' + str(max))
return resultado
Function to fill 'A' matrix
import matriz
def matriz_a(filas, columnas):
matriz = []
cad = ""
for i in range(columnas):
matriz.append([cad.join('A') for _ in range(filas)])
return matriz
var=matriz_a(40,40)
print(matriz.calcular(var))
Random function that does not generate an error.
import string
import random
import matriz
def matriz_random(filas,columnas, chars=string.ascii_uppercase):
matriza=[]
cad=""
for i in range(columnas):
cadena=cad.join(random.choice(chars) for _ in range(filas))
matriza.append(cadena)
return matriza
matrizb=matriz_random(100,100)
print(matriz.calcular(matrizb))