Hello. I try to write the calculation of the determinants of the matrix by the decomposition method. I attach the text of the program and the output of the command line. Python 3.5

def cutmtrx(y, mtrx): for i in range(len(mtrx[0])): mtrx[i].pop(y) mtrx.pop(0) return mtrx def deter(n, mtrx): if n == 2: return mtrx[0][0]*mtrx[1][1]-mtrx[0][1]*mtrx[1][0] elif n >= 3: x = 0 mn = 1 for i in range(n): x += mn*mtrx[0][i]*deter(n-1, cutmtrx(i, mtrx)) mn *= -1 return x n = int(input()) mtrx = [] for i in range(n): mtrx.append(list(map(int, input().split()))) print(deter(n, mtrx)) 

Conclusion:

  C:\Users\Username\Dropbox\Python>python determinant.py 3 1 2 3 4 5 6 7 8 9 Traceback (most recent call last): File "determinant.py", line 24, in <module> print(deter(n, mtrx)) File "determinant.py", line 15, in deter m = deter(n-1, mt) File "determinant.py", line 9, in deter return mtrx[0][0]*mtrx[1][1]-mtrx[0][1]*mtrx[1][0] IndexError: list index out of range C:\Users\Username\Dropbox\Python>python determinant.py 3 1 5 9 4 7 3 2 5 7 Traceback (most recent call last): File "determinant.py", line 22, in <module> print(deter(n, mtrx)) File "determinant.py", line 14, in deter x += mn*mtrx[0][i]*deter(n-1, cutmtrx(i, mtrx)) File "determinant.py", line 9, in deter return mtrx[0][0]*mtrx[1][1]-mtrx[0][1]*mtrx[1][0] IndexError: list index out of range 

    1 answer 1

    Arrays in python are passed by reference. The cutmtrx function spoils the cutmtrx array, after the first access it becomes 2x2 after the second 1x1, this leads to an error exceeding the array.