human
Coordinates of the last cell in a Matrix on which performing given operations exits from the Matrix | Function to check if the indices ( i , j ) are valid indices in a Matrix or not.
Cases for invalid cells.
Return true if valid.
Function to find indices of cells of a matrix from which traversal leads to out of the matrix.
Starting from cell ( 0 , 0 ) , traverse in right direction.
Stores direction changes.
Iterate until the current cell exceeds beyond the matrix.
Current index.
If the current cell is 1.
Update arr [ i ] [ j ] = 0.
Update indices according to the direction.
Otherwise.
Update indices according to the direction.
The exit cooridnates.
Number of rows.
Number of columns.
Given matrix arr [ ] [ ]"
Solution in Python:
 Write me a code to coordinates of the last cell in a Matrix on which performing given operations exits from the Matrix and the other steps required?
assistant
#Here is code to coordinates of the last cell in a Matrix on which performing given operations exits from the Matrix and the other steps required.
 def issafe ( m , n , i , j ) :
	if i < 0 :
		return False
	if j < 0 :
		return False
	if i >= m :
		return False
	if j >= n :
		return False
	return True
def endpoints ( arr , m , n ) :
	i = 0
	j = 0
	current_d = ' r '
	rcd = { ' l ' : ' u ' , ' u ' : ' r ' , ' r ' : ' d ' , ' d ' : ' l ' }
	while issafe ( m , n , i , j ) :
		current_i = i
		current_j = j
		if arr [ i ] [ j ] == 1 :
			move_in = rcd [ current_d ]
			arr [ i ] [ j ] = 0
			if move_in == ' u ' :
				i -= 1
			elif move_in == ' d ' :
				i += 1
			elif move_in == ' l ' :
				j -= 1
			elif move_in == ' r ' :
				j += 1
			current_d = move_in
		else :
			if current_d == ' u ' :
				i -= 1
			elif current_d == ' d ' :
				i += 1
			elif current_d == ' l ' :
				j -= 1
			elif current_d == ' r ' :
				j += 1
	return ( current_i , current_j )
M = 3
N = 5
arr = [ [ 0 , 1 , 1 , 1 , 0 ] , [ 1 , 0 , 1 , 0 , 1 ] , [ 1 , 1 , 1 , 0 , 0 ] , ]
print ( endpoints ( arr , M , N ) )