#πŸ”’ why does it say out of range???

5 messages Β· Page 1 of 1 (latest)

modern terrace
#

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)
grand questBOT
#

Hey @modern terrace!

It looks like you incorrectly specified a language for your code block.

Make sure you put your code on a new line following py. There must not be any spaces after py.

Here is an example of how it should look:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
grand questBOT
#

@modern terrace

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

grand questBOT
#

@modern terrace

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.