Advertisement
GeorgiLukanov87

06. Matrix Shuffling - Multidimensional Lists - Exercise 1

Aug 27th, 2022
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.00 KB | None | 0 0
  1. #  06. Matrix Shuffling
  2.  
  3. #  https://judge.softuni.org/Contests/Practice/Index/1835#5
  4. #  Multidimensional Lists - Exercise 1
  5.  
  6.  
  7. def is_outside(row, col, rows, cols):
  8.     return row < 0 or col < 0 or row >= rows or col >= cols
  9.  
  10.  
  11. size = input().split()
  12. rows = int(size[0])
  13. cols = int(size[1])
  14.  
  15. matrix = []
  16. for _ in range(rows):
  17.     matrix.append(input().split(' '))
  18.  
  19. command = input()
  20. while not command == 'END':
  21.     command = command.split()
  22.  
  23.     if command[0] != 'swap' or len(command) != 5:
  24.         print('Invalid input!')
  25.         command = input()
  26.         continue
  27.  
  28.     row1 = int(command[1])
  29.     col1 = int(command[2])
  30.     row2 = int(command[3])
  31.     col2 = int(command[4])
  32.     if is_outside(row1, col1, rows, cols) or is_outside(row2, col2, rows, cols):
  33.         print('Invalid input!')
  34.         command = input()
  35.         continue
  36.  
  37.     matrix[row1][col1], matrix[row2][col2] = matrix[row2][col2], matrix[row1][col1]
  38.     for el in matrix:
  39.         print(*el)
  40.  
  41.     command = input()
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement