Advertisement
GeorgiLukanov87

useful matrix functions for the exam

Oct 17th, 2022 (edited)
225
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 1 0
  1. # USEFULL MATRIX FUNCTIONS FOR THE EXAM -> SAVING TIME !
  2. -----------------------------------------------------------------------------------------------
  3. # Validating if cell is inside the matrix :
  4. # SIZE_N = SIZE_M (SQUARED MATRIX) 3x3 , 4x4 , 5x5 etc...
  5. def is_inside(row, col):
  6.     return 0 <= row < SIZE and 0 <= col < SIZE
  7.  
  8. # IF CELL IS OUTSIDE! : # not recommendable !
  9. def is_outside(row,col):
  10.     return if row < 0 or row > SIZE - 1 or col < 0 or col > SIZE - 1
  11.  
  12.  
  13. # If matrix not squared: 4x6 , 3x5 , 12x8 etc...
  14. N = size_rows
  15. M = size_cols
  16. def is_inside(r, c):
  17.     return 0 <= row < N and 0 <= col < M
  18. -----------------------------------------------------------------------------------------------
  19. # Get next position/step/move : ( 1 step )
  20. def get_next_position(direction, row, col):
  21.     if direction == 'left':
  22.         return row, col - 1
  23.     elif direction == 'right':
  24.         return row, col + 1
  25.     elif direction == 'up':
  26.         return row - 1, col
  27.     elif direction == 'down':
  28.         return row + 1, col
  29. -----------------------------------------------------------------------------------------------
  30. #Validate if STEP -> After geting the current position and if is OUTSIDE the matrix -> continue to the other side with the same direction :
  31. def validate_step(row, col):
  32.     if row >= SIZE:
  33.         row = 0
  34.     elif row < 0:
  35.         row = (SIZE - 1)
  36.  
  37.     if col >= SIZE:
  38.         col = 0
  39.     elif col < 0:
  40.         col = (SIZE - 1)
  41.     return row, col
  42.  
  43. # OR module divide '%' -> automaticly returns you if the current step goes OUTSIDE \
  44. # and returns you a valid indexes on the other side of the matrix with the same direction .
  45. def get_next_position(direction, row, col):
  46.     if direction == 'left':
  47.         return row % SIZE, (col - 1)% SIZE
  48.     elif direction == 'right':
  49.         return row % SIZE, (col + 1)% SIZE
  50.     elif direction == 'up':
  51.         return (row - 1)% SIZE, col % SIZE
  52.     elif direction == 'down':
  53.         return (row + 1)%SIZE, col % SIZE
  54. -----------------------------------------------------------------------------------------------
  55.  
  56. # All tuple-indexes in a single LIST wich surrounding a cell :
  57. #                                          |            DIAGONALS :
  58. #         right    left      Up      down  |r-down, right-up, left-up, left-down
  59. moves = [(0, 1), (0, -1), (-1, 0), (1, 0), (1, 1), (-1, 1), (-1, -1), (1, -1)]
  60.  
  61. -----------------------------------------------------------------------------------------------
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement