Answer to Question #287417 in Python for Rox

Question #287417

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.


1
Expert's answer
2022-01-14T11:25:24-0500
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)


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS