I am writing a simple dynamic programming algorithm to traverse a matrix in Python. Due to my lack of understanding in Python's scoping rules, I have difficulty fixing this bug.
Here's a part of my code:
# a new null matrix.
footstep = []
for i in range(size):
row = [0]*size
footstep.append(row)
def min_val(m, n, footstep):
# copy a new footstep 2D-matrix.
fs = list(footstep)
if ((m == 0) and (n == 0)):
print "origin"
return get_grid(0, 0)
......
......
......
return (min(min_val(m-1, n, fs), min_val(m, n-1, fs)
print min_val(4, 4, footstep)
print footstep
At the start of the function call I copied a new identical footstep list. Therefore, my expectation is that : the footstep list in the global scope must not have changed.
How should I fix my code? Please help.