Advertisement
viligen

matrix_shuffling

Jan 23rd, 2022
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | None | 0 0
  1. def not_valid_coordinates(anyrow1, anycol1, anyrow2, anycol2, anyrows, anycols):
  2.     return anyrow1 < 0 or anyrow2 < 0 or anycol1 < 0 or anycol2 < 0 \
  3.             or anyrows <= anyrow1 or anyrows <= anyrow2 or anycols <= anycol1 or anycols <= anycol2
  4.  
  5.  
  6. rows, cols = [int(n) for n in input().split()]
  7.  
  8. matrix = []
  9. for r in range(rows):
  10.     current_row = input().split()
  11.     matrix.append(current_row)
  12.  
  13. while True:
  14.     command_line = input().split()
  15.     if "END" in command_line:
  16.         break
  17.  
  18.     elif len(command_line) != 5 or command_line[0] != "swap":
  19.         print("Invalid input!")
  20.         continue
  21.  
  22.     row1, col1, row2, col2 = [int(n) for n in command_line[1:]]
  23.     if not_valid_coordinates(row1, col1, row2, col2, rows, cols):
  24.         print("Invalid input!")
  25.         continue
  26.  
  27.     matrix[row1][col1], matrix[row2][col2] = matrix[row2][col2], matrix[row1][col1]
  28.     for row in range(rows):
  29.         print(*matrix[row])
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement