Answer to Question #224628 in Python for saaiii

Question #224628

Zig-zag order in matrix;;


Input:---

4 4

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16


Output :--

1 2 3 4 5 6 7 8 9 10 11 12 16 15 14 13


Input 2:---


3 4

1 2 3 4

10 11 12 5

9 8 7 6


Output 2:--


1 2 3 4 5 12 11 10 9 8 7 6


1
Expert's answer
2021-08-09T11:06:26-0400
matrix =[
            [1, 2, 3, 4],


            [5, 6, 7, 8],
            
            [9, 10, 11, 12],
            
            [13, 14, 15, 16]
        ]
r=4
c=4
    
s=[[] for i in range(r+c-1)]
  
for i in range(r):
    for j in range(c):
        sum1=i+j
        if(sum1%2 ==0):
            s[sum1].insert(0,matrix[i][j])
        else:
            s[sum1].append(matrix[i][j])
          
for i in s:
    for j in i:
        print(j,end=" ")
          

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