C1W2 Back substitution

I have an error after run a code: w2_unittest.test_back_substitution(back_substitution)

An exception was thrown while running your function: index 3 is out of bounds for axis 0 with size 3.
Input matrix:
[[1 0 0 5]
[0 1 0 6]
[0 0 1 7]]

I don’t know how to solve it.
Please suggest me which line of the codes I should concern in exercise 2.

1 Like

Hi @phuttipan!
Unfortunately, the information provided is not enough for me to help you to debug this error, could you please provide more details?

My best guess would be that in your matrix you miss commas after each number and matrix, try replacing your matrix with the one below:

[[1, 0, 0, 5],
[0, 1, 0, 6],
[0, 0, 1, 7]]

if it doesn’t help, please provide a bit more details.

1 Like

{moderator edit - solution code removed}

I’m not sure is it illegal to this community or not to post the codes

1 Like

It is not permitted to publicly share solution code. But it does help for debugging. I will edit the post to remove the code.

Why you adding 1 to row in the loop? Print the value of row. Because of the reversed(range) the first value will be the last row, so if you add one you are “off the end”, which is exactly what the error message is telling you, right?

Also remember that indexing is “0 based” in python. So if a matrix has 3 rows, the last row has index 2.

1 Like

You right

Although I remove adding 1 out, I still get lost and not even know how to do next.

1 Like

Debugging is part of the job. The first step would be write out the first test case with pencil and paper and then go through the steps of the solution “by hand”, so that you know what should be happening at each stage. Now the question is how to figure out why your code is not correctly implementing that procedure. Just add print statements at each step to show what is going on. The value of row and the value of j and the actual row values before and after the updates.

One thing to note is that substitution_row is intended to be the actual row of the matrix, but that’s not what is happening with the way you originally wrote the code. You just set it to a row number, which is not what was intended.

2 Likes

I’ll try it. Thank you so much for your suggestion.

1 Like