Write a program to read the elements of a sparse matrix from the user and display it
in its 3-tuple form. Your program should verify whether at the entire matrix is
sparse or not.
bar = [[0,0,3,0,4],[0,0,5,7,0],[0,0,0,0,0],[0,2,6,0,0]]
sz = 0
for i in range(4):
for j in range(5):
if (bar[i][j] != 0):
sz += 1
rows, cols = (3, sz)
foo = [[0 for i in range(cols)] for j in range(rows)]
k = 0
for i in range(4):
for j in range(5):
if (bar[i][j] != 0):
foo[0][k] = i
foo[1][k] = j
foo[2][k] = bar[i][j]
k += 1
for i in foo:
print(i)
Comments
Leave a comment