Advertisement
simeonshopov

Matrix Modification

May 26th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. def is_valid(i: int):
  2.     return 0 <= i < size
  3.  
  4.  
  5. size = int(input())
  6.  
  7. matrix = [[int(x) for x in input().split()] for _ in range(size)]
  8.  
  9. while True:
  10.     command = input()
  11.     if command == 'END':
  12.         break
  13.     tokens = command.split()
  14.     row = int(tokens[1])
  15.     col = int(tokens[2])
  16.     value = int(tokens[3])
  17.     if tokens[0] == 'Add':
  18.         if is_valid(row) and is_valid(col):
  19.             matrix[row][col] += value
  20.         else:
  21.             print('Invalid coordinates')
  22.     elif tokens[0] == 'Subtract':
  23.         if is_valid(row) and is_valid(col):
  24.             matrix[row][col] -= value
  25.         else:
  26.             print('Invalid coordinates')
  27.  
  28. [print(' '.join(map(str, x))) for x in matrix]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement