does anyone know why this code to generate a matrix nxn with some restrains generate an out of range in matric[row][col] = new_value?
# Base case: if we've filled the entire matrix, print it
if len(matrix) == size:
print(matrix)
return
# Find the current maximum value in the matrix
max_value = 0
for row_values in matrix:
for value in row_values:
if max_value < value:
max_value = value
# Try placing new values in the current cell
for new_value in range(max_value, size):
# Note: These conditions need revision as they can cause index out of range errors
if row == size and col == size:
matrix[row][col] = new_value
generate_ordered_matrices(matrix, size, row+1, col+1)
matrix[row][col] = 0
elif row == size and col != size:
matrix[row][col] = new_value
generate_ordered_matrices(matrix, size, row, col+1)
matrix[row][col] = 0
elif row != size and col != size:
matrix[row][col] = new_value
generate_ordered_matrices(matrix, size, row+1, col+1)
matrix[row][col] = 0
elif row == size and col != size: # This condition is redundant
matrix[row][col] = new_value
generate_ordered_matrices(matrix, size, row+1, col)
matrix[row][col] = 0
# Initialize and call the function
generate_ordered_matrices([], 2, 0, 0)