






'''simple docstring'''




class    A_	:
							"""simple docstring"""




							def __init__(		self			:List[Any]				)				->  Tuple:


										'''simple docstring'''




										snake_case_				:					Any   =       ""
										snake_case_				:					List[Any]   =       ""
										snake_case_				:					Dict   =       []

							def 							_A	(		self			:str       ,							lowerCAmelCase__			:int       ,							lowerCAmelCase__			:int				)				->  int:


										'''simple docstring'''




										if m == -1:
													return n + 1
										elif n == -1:
													return m + 1
										elif self.dp[m][n] > -1:
													return self.dp[m][n]
										else:
													if self.worda[m] == self.worda[n]:
																snake_case_				:					int   =       self.__min_dist_top_down_dp(m - 1       ,							n - 1				)
													else:
																snake_case_				:					Any   =       self.__min_dist_top_down_dp(lowerCAmelCase__       ,							n - 1				)
																snake_case_				:					Optional[Any]   =       self.__min_dist_top_down_dp(m - 1       ,							lowerCAmelCase__				)
																snake_case_				:					Any   =       self.__min_dist_top_down_dp(m - 1       ,							n - 1				)
																snake_case_				:					Union[str, Any]   =       1 + min(lowerCAmelCase__       ,							lowerCAmelCase__       ,							lowerCAmelCase__				)

													return self.dp[m][n]

							def 							_A	(		self			:Any       ,							lowerCAmelCase__			:str       ,							lowerCAmelCase__			:str				)				->  int:


										'''simple docstring'''




										snake_case_				:					int   =       worda
										snake_case_				:					Optional[int]   =       worda
										snake_case_				:					int   =       [[-1 for _ in range(len(lowerCAmelCase__				)				)] for _ in range(len(lowerCAmelCase__				)				)]

										return self.__min_dist_top_down_dp(len(lowerCAmelCase__				) - 1       ,							len(lowerCAmelCase__				) - 1				)






							def 							_A	(		self			:Optional[int]       ,							lowerCAmelCase__			:str       ,							lowerCAmelCase__			:str				)				->  int:


										'''simple docstring'''




										snake_case_				:					List[Any]   =       worda
										snake_case_				:					Optional[int]   =       worda
										snake_case_				:					Union[str, Any]   =       len(lowerCAmelCase__				)
										snake_case_				:					List[str]   =       len(lowerCAmelCase__				)
										snake_case_				:					str   =       [[0 for _ in range(n + 1				)] for _ in range(m + 1				)]

										for i in range(m + 1				):
													for j in range(n + 1				):
																if i == 0:  # first string is empty
																			snake_case_				:					Optional[Any]   =       j
																elif j == 0:  # second string is empty
																			snake_case_				:					List[Any]   =       i
																elif worda[i - 1] == worda[j - 1]:  # last characters are equal
																			snake_case_				:					Optional[int]   =       self.dp[i - 1][j - 1]
																else:
																			snake_case_				:					int   =       self.dp[i][j - 1]
																			snake_case_				:					List[str]   =       self.dp[i - 1][j]
																			snake_case_				:					List[Any]   =       self.dp[i - 1][j - 1]
																			snake_case_				:					Any   =       1 + min(lowerCAmelCase__       ,							lowerCAmelCase__       ,							lowerCAmelCase__				)
										return self.dp[m][n]


if __name__ == "__main__":
		__lowerCamelCase   :					Any   							=					EditDistance()

		print('''****************** Testing Edit Distance DP Algorithm ******************''')
		print()

		__lowerCamelCase   :					Dict   							=					input('''Enter the first string: ''').strip()
		__lowerCamelCase   :					int   							=					input('''Enter the second string: ''').strip()

		print()
		print(f'''The minimum edit distance is: {solver.min_dist_top_down(Sa, Sa)}''')
		print(f'''The minimum edit distance is: {solver.min_dist_bottom_up(Sa, Sa)}''')
		print()
		print('''*************** End of Testing Edit Distance DP Algorithm ***************''')












'''simple docstring'''




import os
import sys
import tempfile

import torch

from .state import AcceleratorState
from .utils import PrecisionType, PrepareForLaunch, is_mps_available, patch_environment

def 	__UpperCAmelCase       ( __magic_name__     ,__magic_name__=()     ,__magic_name__=None     ,__magic_name__="no"     ,__magic_name__="29500"						)->    Optional[int]:



			"""simple docstring"""



			snake_case_				:					str   =       False
			snake_case_				:					int   =       False
			if any(key.startswith("KAGGLE"						) for key in os.environ.keys()						):
						snake_case_				:					Any   =       True
			elif "IPython" in sys.modules:
						snake_case_				:					Union[str, Any]   =       "google.colab" in str(sys.modules["IPython"].get_ipython()						)

			try:
						snake_case_				:					Any   =       PrecisionType(mixed_precision.lower()						)
			except ValueError:
						raise ValueError(
						    F'''Unknown mixed_precision mode: {args.mixed_precision.lower()}. Choose between {PrecisionType.list()}.'''						)

			if (in_colab or in_kaggle) and (os.environ.get("TPU_NAME"     ,__magic_name__						) is not None):
						# TPU launch
						import torch_xla.distributed.xla_multiprocessing as xmp

						if len(AcceleratorState._shared_state						) > 0:
									raise ValueError(
									    "To train on TPU in Colab or Kaggle Kernel, the `Accelerator` should only be initialized inside "
									    "your training function. Restart your notebook and make sure no cells initializes an "
									    "`Accelerator`."						)
						if num_processes is None:
									snake_case_				:					Tuple   =       8

						snake_case_				:					Optional[int]   =       PrepareForLaunch(__magic_name__     ,distributed_type="TPU"						)
						print(F'''Launching a training on {num_processes} TPU cores.'''						)
						xmp.spawn(__magic_name__     ,args=__magic_name__     ,nprocs=__magic_name__     ,start_method="fork"						)
			elif in_colab:
						# No need for a distributed launch otherwise as it's either CPU or one GPU.
						if torch.cuda.is_available():
									print("Launching training on one GPU."						)
						else:
									print("Launching training on one CPU."						)
						function(*__magic_name__						)
			else:
						if num_processes is None:
									raise ValueError(
									    "You have to specify the number of GPUs you would like to use, add `num_processes=...` to your call."						)

						if num_processes > 1:
									# Multi-GPU launch
									from torch.multiprocessing import start_processes
									from torch.multiprocessing.spawn import ProcessRaisedException

									if len(AcceleratorState._shared_state						) > 0:
												raise ValueError(
												    "To launch a multi-GPU training from your notebook, the `Accelerator` should only be initialized "
												    "inside your training function. Restart your notebook and make sure no cells initializes an "
												    "`Accelerator`."						)

									if torch.cuda.is_initialized():
												raise ValueError(
												    "To launch a multi-GPU training from your notebook, you need to avoid running any instruction "
												    "using `torch.cuda` in any cell. Restart your notebook and make sure no cells use any CUDA "
												    "function."						)

									# torch.distributed will expect a few environment variable to be here. We set the ones common to each
									# process here (the other ones will be set be the launcher).
									with patch_environment(
									    world_size=__magic_name__     ,master_addr="127.0.01"     ,master_port=__magic_name__     ,mixed_precision=__magic_name__						):
												snake_case_				:					Optional[int]   =       PrepareForLaunch(__magic_name__     ,distributed_type="MULTI_GPU"						)
												print(F'''Launching training on {num_processes} GPUs.'''						)
												try:
															start_processes(__magic_name__     ,args=__magic_name__     ,nprocs=__magic_name__     ,start_method="fork"						)
												except ProcessRaisedException as e:
															if "Cannot re-initialize CUDA in forked subprocess" in e.args[0]:
																		raise RuntimeError(
																		    "CUDA has been initialized before the `notebook_launcher` could create a forked subprocess. "
																		    "This likely stems from an outside import causing issues once the `notebook_launcher()` is called. "
																		    "Please review your imports and test them when running the `notebook_launcher()` to identify "
																		    "which one is problematic."						) from e

						else:
									# No need for a distributed launch otherwise as it's either CPU, GPU or MPS.
									if is_mps_available():
												snake_case_				:					Any   =       "1"
												print("Launching training on MPS."						)
									elif torch.cuda.is_available():
												print("Launching training on one GPU."						)
									else:
												print("Launching training on CPU."						)
									function(*__magic_name__						)






def 	__UpperCAmelCase       ( __magic_name__     ,__magic_name__=()     ,__magic_name__=2						)->    Dict:



			"""simple docstring"""



			from torch.multiprocessing import start_processes

			with tempfile.NamedTemporaryFile() as tmp_file:
						# torch.distributed will expect a few environment variable to be here. We set the ones common to each
						# process here (the other ones will be set be the launcher).
						with patch_environment(
						    world_size=__magic_name__     ,master_addr="127.0.01"     ,master_port="29500"     ,accelerate_mixed_precision="no"     ,accelerate_debug_rdv_file=tmp_file.name     ,accelerate_use_cpu="yes"     ,):
									snake_case_				:					Any   =       PrepareForLaunch(__magic_name__     ,debug=__magic_name__						)
									start_processes(__magic_name__     ,args=__magic_name__     ,nprocs=__magic_name__     ,start_method="fork"						)




