human
Print the indices for every row of a grid from which escaping from the grid is possible | Function to find the row index for every row of a matrix from which one can exit the grid after entering from left.
Iterate over the range [ 0 , M - 1 ].
Stores the direction from which a person enters a cell.
Row index from which one enters the grid.
Column index from which one enters the grid.
Iterate until j is atleast N - 1.
If Mat [ i ] [ j ] is equal to 1.
If entry is from left cell.
Decrement i by 1.
Assign ' D ' to dir.
If entry is from upper cell.
Decrement j by 1.
Assign ' R ' to dir.
If entry is from right cell.
Increment i by 1.
Assign ' U ' to dir.
If entry is from bottom cell.
Increment j by 1.
Assign ' L ' to dir.
Otherwise ,.
If entry is from left cell.
Increment i by 1.
Assign ' U ' to dir.
If entry is from upper cell.
Increment j by 1.
Assign ' L ' to dir.
If entry is from right cell.
Decrement i by 1.
Assign ' D ' to dir.
If entry is from lower cell.
Decrement j by 1.
Assign ' R ' to dir.
If i or j is less than 0 or i is equal to M or j is equal to N.
If j is equal to N.
Otherwise.
Input.
Function call"
How can the above be solved in Python?
assistant
#Here is code to print the indices for every row of a grid from which escaping from the grid is possible and the other steps required.
 def findPath ( arr , M , N ) :
	for row in range ( M ) :
		dir = ' L '
		i = row
		j = 0
		while ( j < N ) :
			if ( arr [ i ] [ j ] == 1 ) :
				if ( dir == ' L ' ) :
					i -= 1
					dir = ' D '
				elif ( dir == ' U ' ) :
					j -= 1
					dir = ' R '
				elif ( dir == ' R ' ) :
					i += 1
					dir = ' U '
				elif ( dir == ' D ' ) :
					j += 1
					dir = ' L '
			else :
				if ( dir == ' L ' ) :
					i += 1
					dir = ' U '
				elif ( dir == ' U ' ) :
					j += 1
					dir = ' L '
				elif ( dir == ' R ' ) :
					i -= 1
					dir = ' D '
				elif ( dir == ' D ' ) :
					j -= 1
					dir = ' R '
			if ( i < 0 or i == M or j < 0 or j == N ) :
				break
		if ( j == N ) :
			print ( i , end = " ▁ " )
		else :
			print ( - 1 , end = " ▁ " )
arr = [ [ 1 , 1 , 0 , 1 ] , [ 1 , 1 , 0 , 0 ] , [ 1 , 0 , 0 , 0 ] , [ 1 , 1 , 0 , 1 ] , [ 0 , 1 , 0 , 1 ] ]
M = len ( arr )
N = len ( arr [ 0 ] )
findPath ( arr , M , N )