Skip to content

Instantly share code, notes, and snippets.

@pruthvi6767
Created February 23, 2020 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pruthvi6767/1a6a3cfe442bd6808a15b4b05a6fa2b7 to your computer and use it in GitHub Desktop.
Save pruthvi6767/1a6a3cfe442bd6808a15b4b05a6fa2b7 to your computer and use it in GitHub Desktop.
finds matrix B from cumulative sum of given matrix A(mxn)
a = [[1,2,3,4],[3,4,5,6],[234,456,555,6789]]
b = [[0 for _ in range(len(a[0]))] for _ in range(len(a)) ]
def build_b(pos):
i, j = pos
if i ==0 and j == 0:
b[i][j] = a[0][0]
return b[i][j]
elif b[i][j] !=0:
return b[i][j]
elif i == 0 and j < len(a[0]):
b[i][j] = build_b((i,j-1)) + a[i][j]
elif j == 0 and i < len(a):
b[i][j] = build_b((i-1,len(a[0])-1)) + a[i][j]
else:
b[i][j] = build_b((i,j-1))+a[i][j]
print(b[i][j], a[i][j])
return b[i][j]
# for i in range(len(a)):
# for j in range(len(a[0])):
# b[i][j] = build_b((i,j))
build_b((2,3))
print(b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment