





from __future__ import annotations

snake_case__					:							Dict													=     [
    [-1, 0],  # left
    [0, -1],  # down
    [1, 0],  # right
    [0, 1],  # up
]



def _a				(	lowerCamelCase:   list[list[int]]					,				lowerCamelCase:   list[int]					,				lowerCamelCase:   list[int]					,				lowerCamelCase:   int					,				lowerCamelCase:   list[list[int]]					,				)	->						tuple[list[list[int]], list[list[int]]]:







      '''simple docstring'''






      __A          =				[
          [0 for col in range(len(grid[0] ) )] for row in range(len(lowerCamelCase ) )
      ]  # the reference grid
      __A          =				1
      __A          =				[
          [0 for col in range(len(grid[0] ) )] for row in range(len(lowerCamelCase ) )
      ]  # the action grid

      __A          =				init[0]
      __A          =				init[1]
      __A          =				0
      __A          =				g + heuristic[x][y]  # cost from starting cell to destination cell
      __A          =				[[f, g, x, y]]

      __A          =				False  # flag that is set when search is complete
      __A          =				False  # flag set if we can't find expand

      while not found and not resign:
            if len(lowerCamelCase ) == 0:
                  raise ValueError('''Algorithm is unable to find solution''' )
            else:  # to choose the least costliest action so as to move closer to the goal
                  cell.sort()
                  cell.reverse()
                  __A          =				cell.pop()
                  __A          =				next_cell[2]
                  __A          =				next_cell[3]
                  __A          =				next_cell[1]

                  if x == goal[0] and y == goal[1]:
                        __A          =				True
                  else:
                        for i in range(len(lowerCamelCase ) ):  # to try out different valid actions
                              __A          =				x + DIRECTIONS[i][0]
                              __A          =				y + DIRECTIONS[i][1]
                              if xa >= 0 and xa < len(lowerCamelCase ) and ya >= 0 and ya < len(grid[0] ):
                                    if closed[xa][ya] == 0 and grid[xa][ya] == 0:
                                          __A          =				g + cost
                                          __A          =				ga + heuristic[xa][ya]
                                          cell.append([fa, ga, xa, ya] )
                                          __A          =				1
                                          __A          =				i
      __A          =				[]
      __A          =				goal[0]
      __A          =				goal[1]
      invpath.append([x, y] )  # we get the reverse path from here
      while x != init[0] or y != init[1]:
            __A          =				x - DIRECTIONS[action[x][y]][0]
            __A          =				y - DIRECTIONS[action[x][y]][1]
            __A          =				xa
            __A          =				ya
            invpath.append([x, y] )

      __A          =				[]
      for i in range(len(lowerCamelCase ) ):
            path.append(invpath[len(lowerCamelCase ) - 1 - i] )
      return path, action


if __name__ == "__main__":
       snake_case__					:							List[Any]													=     [
           [0, 1, 0, 0, 0, 0],
           [0, 1, 0, 0, 0, 0],  # 0 are free path whereas 1's are obstacles
           [0, 1, 0, 0, 0, 0],
           [0, 1, 0, 0, 1, 0],
           [0, 0, 0, 0, 1, 0],
       ]

       snake_case__					:							List[Any]													=     [0, 0]
       # all coordinates are given in format [y,x]
       snake_case__					:							int													=     [len(grid) - 1, len(grid[0]) - 1]
       snake_case__					:							Optional[Any]													=     1

       # the cost map which pushes the path closer to the goal
       snake_case__					:							Dict													=     [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
       for i in range(len(grid)):
              for j in range(len(grid[0])):
                     snake_case__					:							List[Any]													=     abs(i - goal[0]) + abs(j - goal[1])
                     if grid[i][j] == 1:
                            # added extra penalty in the heuristic map
                            snake_case__					:							int													=     99

       snake_case__						,    snake_case__					:							Any													=     search(grid, init, goal, cost, heuristic)

       print('ACTION MAP')
       for i in range(len(action)):
              print(action[i])

       for i in range(len(path)):
              print(path[i])












import numpy as np
from PIL import Image



def _a				(	lowerCamelCase:   np.ndarray					,				lowerCamelCase:   int					,				lowerCamelCase:   int )	->						np.ndarray:







      '''simple docstring'''






      __A          =				np.array(lowerCamelCase )
      if arr.shape[0] != arr.shape[1]:
            raise ValueError('''The input array is not a square matrix''' )
      __A          =				0
      __A          =				0
      __A          =				0
      __A          =				0

      # compute the shape of the output matrix
      __A          =				(arr.shape[0] - size) // stride + 1
      # initialize the output matrix with zeros of shape maxpool_shape
      __A          =				np.zeros((maxpool_shape, maxpool_shape) )

      while i < arr.shape[0]:
            if i + size > arr.shape[0]:
                  # if the end of the matrix is reached, break
                  break
            while j < arr.shape[1]:
                  # if the end of the matrix is reached, break
                  if j + size > arr.shape[1]:
                        break
                  # compute the maximum of the pooling matrix
                  __A          =				np.max(arr[i : i + size, j : j + size] )
                  # shift the pooling matrix by stride of column pixels
                  j += stride
                  mat_j += 1

            # shift the pooling matrix by stride of row pixels
            i += stride
            mat_i += 1

            # reset the column index to 0
            __A          =				0
            __A          =				0

      return updated_arr



def _a				(	lowerCamelCase:   np.ndarray					,				lowerCamelCase:   int					,				lowerCamelCase:   int )	->						np.ndarray:







      '''simple docstring'''






      __A          =				np.array(lowerCamelCase )
      if arr.shape[0] != arr.shape[1]:
            raise ValueError('''The input array is not a square matrix''' )
      __A          =				0
      __A          =				0
      __A          =				0
      __A          =				0

      # compute the shape of the output matrix
      __A          =				(arr.shape[0] - size) // stride + 1
      # initialize the output matrix with zeros of shape avgpool_shape
      __A          =				np.zeros((avgpool_shape, avgpool_shape) )

      while i < arr.shape[0]:
            # if the end of the matrix is reached, break
            if i + size > arr.shape[0]:
                  break
            while j < arr.shape[1]:
                  # if the end of the matrix is reached, break
                  if j + size > arr.shape[1]:
                        break
                  # compute the average of the pooling matrix
                  __A          =				int(np.average(arr[i : i + size, j : j + size] ) )
                  # shift the pooling matrix by stride of column pixels
                  j += stride
                  mat_j += 1

            # shift the pooling matrix by stride of row pixels
            i += stride
            mat_i += 1
            # reset the column index to 0
            __A          =				0
            __A          =				0

      return updated_arr


# Main Function
if __name__ == "__main__":
       from doctest import testmod

       testmod(name='avgpooling', verbose=True)

       # Loading the image
       snake_case__					:							Optional[Any]													=     Image.open('path_to_image')

       # Converting the image to numpy array and maxpooling, displaying the result
       # Ensure that the image is a square matrix

       Image.fromarray(maxpooling(np.array(image), size=3, stride=2)).show()

       # Converting the image to numpy array and averagepooling, displaying the result
       # Ensure that the image is a square matrix

       Image.fromarray(avgpooling(np.array(image), size=3, stride=2)).show()





