Advertisement
viligen

matrix_modification

Jan 24th, 2022
901
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def valid_idx(any_row, any_col, size):
  2.     valid_range = range(0, size)
  3.     if any_row in valid_range and any_col in valid_range:
  4.         return True
  5.  
  6.  
  7. rows = int(input())
  8. matrix = []
  9. for row in range(rows):
  10.     matrix.append([int(n) for n in input().split()])
  11.  
  12. while True:
  13.     command_line = input().split()
  14.     if command_line[0] == "END":
  15.         break
  16.     command = command_line[0]
  17.     r, c, value = [int(n) for n in command_line[1:]]
  18.     if not valid_idx(r, c, rows):
  19.         print("Invalid coordinates")
  20.         continue
  21.     if command == "Add":
  22.         matrix[r][c] += value
  23.     elif command == "Subtract":
  24.         matrix[r][c] -= value
  25.  
  26. for row in range(rows):
  27.     print(*matrix[row])
  28.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement