Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # USEFULL MATRIX FUNCTIONS FOR THE EXAM -> SAVING TIME !
- -----------------------------------------------------------------------------------------------
- # Validating if cell is inside the matrix :
- # SIZE_N = SIZE_M (SQUARED MATRIX) 3x3 , 4x4 , 5x5 etc...
- def is_inside(row, col):
- return 0 <= row < SIZE and 0 <= col < SIZE
- # IF CELL IS OUTSIDE! : # not recommendable !
- def is_outside(row,col):
- return if row < 0 or row > SIZE - 1 or col < 0 or col > SIZE - 1
- # If matrix not squared: 4x6 , 3x5 , 12x8 etc...
- N = size_rows
- M = size_cols
- def is_inside(r, c):
- return 0 <= row < N and 0 <= col < M
- -----------------------------------------------------------------------------------------------
- # Get next position/step/move : ( 1 step )
- def get_next_position(direction, row, col):
- if direction == 'left':
- return row, col - 1
- elif direction == 'right':
- return row, col + 1
- elif direction == 'up':
- return row - 1, col
- elif direction == 'down':
- return row + 1, col
- -----------------------------------------------------------------------------------------------
- #Validate if STEP -> After geting the current position and if is OUTSIDE the matrix -> continue to the other side with the same direction :
- def validate_step(row, col):
- if row >= SIZE:
- row = 0
- elif row < 0:
- row = (SIZE - 1)
- if col >= SIZE:
- col = 0
- elif col < 0:
- col = (SIZE - 1)
- return row, col
- # OR module divide '%' -> automaticly returns you if the current step goes OUTSIDE \
- # and returns you a valid indexes on the other side of the matrix with the same direction .
- def get_next_position(direction, row, col):
- if direction == 'left':
- return row % SIZE, (col - 1)% SIZE
- elif direction == 'right':
- return row % SIZE, (col + 1)% SIZE
- elif direction == 'up':
- return (row - 1)% SIZE, col % SIZE
- elif direction == 'down':
- return (row + 1)%SIZE, col % SIZE
- -----------------------------------------------------------------------------------------------
- # All tuple-indexes in a single LIST wich surrounding a cell :
- # | DIAGONALS :
- # right left Up down |r-down, right-up, left-up, left-down
- moves = [(0, 1), (0, -1), (-1, 0), (1, 0), (1, 1), (-1, 1), (-1, -1), (1, -1)]
- -----------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement