Answer to Question #224895 in Python for sayyii

Question #224895

please see the output and correct the code :--

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


def read_matrix():

  line = input()

  words =line.split()

  n = int(words[0])

  m = int(words[1])

  mat = []

  for i in range(n):

      row = []

      line = input()

      words = line.split()

      for j in range(m):

          row.append(int(words[j]))

      mat.append(row)

  return mat

def print_zig_zag(mat):

   for i in mat:

       for j in i:

           print(j,end=" ")


print_zig_zag(read_matrix())



1
Expert's answer
2021-08-10T11:51:53-0400


mat =[
            [ 1, 2, 3,4],
            [ 5, 6, 7,8 ],
            [ 9, 10, 11,12 ], [ 13, 14, 15,16],
        ]
rowsOfMatrix =4
col=4
    
answer =[[] for n in range(rowsOfMatrix+col-1)]
  
for x in range(rowsOfMatrix):
    for y in range(col):
        sum1=x+y
        if(sum1%2 ==0):
  
            
            answer[sum1].insert(0,mat[x][y])
        else:
  
            
            answer[sum1].append(mat[x][y])
          
              


for a in answer:
    for b in a:
        print(b,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