






"""simple docstring"""



import numpy as np
from PIL import Image



def 						lowercase    (				__snake_case				: np.ndarray		,					__snake_case				: int		,					__snake_case				: int	):
		lowercase_ : Tuple							       =   np.array(__snake_case	)
		if arr.shape[0] != arr.shape[1]:
				raise ValueError('''The input array is not a square matrix'''	)
		lowercase_ : Dict							       =   0
		lowercase_ : Any							       =   0
		lowercase_ : List[str]							       =   0
		lowercase_ : Union[str, Any]							       =   0

		# compute the shape of the output matrix
		lowercase_ : Optional[int]							       =   (arr.shape[0] - size) // stride + 1
		# initialize the output matrix with zeros of shape maxpool_shape
		lowercase_ : Union[str, Any]							       =   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
						lowercase_ : Tuple							       =   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
				lowercase_ : Any							       =   0
				lowercase_ : Optional[Any]							       =   0

		return updated_arr



def 						lowercase    (				__snake_case				: np.ndarray		,					__snake_case				: int		,					__snake_case				: int	):
		lowercase_ : int							       =   np.array(__snake_case	)
		if arr.shape[0] != arr.shape[1]:
				raise ValueError('''The input array is not a square matrix'''	)
		lowercase_ : int							       =   0
		lowercase_ : Dict							       =   0
		lowercase_ : Tuple							       =   0
		lowercase_ : Tuple							       =   0

		# compute the shape of the output matrix
		lowercase_ : List[str]							       =   (arr.shape[0] - size) // stride + 1
		# initialize the output matrix with zeros of shape avgpool_shape
		lowercase_ : Optional[int]							       =   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
						lowercase_ : str							       =   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
				lowercase_ : int							       =   0
				lowercase_ : str							       =   0

		return updated_arr


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

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

						# Loading the image
						__A		:       List[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()









class 		UpperCAmelCase   :







						'''simple docstring'''





						def __init__(						self							:     Dict						):






							"""simple docstring"""






							snake_case_		    =     {}  # Mapping from char to TrieNode
							snake_case_		    =     False






						def        snake_case__						(						self							:     Dict       ,	__lowercase							:     list[str]						):






							"""simple docstring"""






							for word in words:
								self.insert(__lowercase						)






						def        snake_case__						(						self							:     List[str]       ,	__lowercase							:     str						):






							"""simple docstring"""






							snake_case_		    =     self
							for char in word:
								if char not in curr.nodes:
									snake_case_		    =     TrieNode()
								snake_case_		    =     curr.nodes[char]
							snake_case_		    =     True






						def        snake_case__						(						self							:     List[Any]       ,	__lowercase							:     str						):






							"""simple docstring"""






							snake_case_		    =     self
							for char in word:
								if char not in curr.nodes:
									return False
								snake_case_		    =     curr.nodes[char]
							return curr.is_leaf






						def        snake_case__						(						self							:     Optional[Any]       ,	__lowercase							:     str						):






							"""simple docstring"""






							def _delete(__lowercase							:     TrieNode       ,	__lowercase							:     str       ,	__lowercase							:     int						) -> bool:
								if index == len(__lowercase						):
									# If word does not exist
									if not curr.is_leaf:
										return False
									snake_case_		    =     False
									return len(curr.nodes						) == 0
								snake_case_		    =     word[index]
								snake_case_		    =     curr.nodes.get(__lowercase						)
								# If char not in current trie node
								if not char_node:
									return False
								# Flag to check if node can be deleted
								snake_case_		    =     _delete(__lowercase       ,	__lowercase       ,	index + 1						)
								if delete_curr:
									del curr.nodes[char]
									return len(curr.nodes						) == 0
								return delete_curr

							_delete(self       ,	__lowercase       ,	0						)







def 						lowerCamelCase__    (		_A     ,	_A							):







	'''simple docstring'''





	if node.is_leaf:
		print(_A     ,	end=" "							)

	for key, value in node.nodes.items():
		print_words(_A     ,	word + key							)


def 						lowerCamelCase__    (		):







	'''simple docstring'''





	snake_case_		    =     "banana bananas bandana band apple all beast".split()
	snake_case_		    =     TrieNode()
	root.insert_many(_A							)
	# print_words(root, "")
	assert all(root.find(_A							) for word in words							)
	assert root.find("banana"							)
	assert not root.find("bandanas"							)
	assert not root.find("apps"							)
	assert root.find("apple"							)
	assert root.find("all"							)
	root.delete("all"							)
	assert not root.find("all"							)
	root.delete("banana"							)
	assert not root.find("banana"							)
	assert root.find("bananas"							)
	return True


def 						lowerCamelCase__    (		_A     ,	_A							):







	'''simple docstring'''





	print(str(_A							)     ,	"works!" if passes else "doesn't work :("							)


def 						lowerCamelCase__    (		):







	'''simple docstring'''





	assert test_trie()


def 						lowerCamelCase__    (		):







	'''simple docstring'''





	print_results("Testing trie functionality"     ,	test_trie()							)


if __name__ == "__main__":
		main()
