
import string

import numpy







def        A__					(						lowerCamelCase     ,    lowerCamelCase						)  ->	int:
				return b if a == 0 else greatest_common_divisor(b % a     ,    lowerCamelCase						)

class        _UpperCamelCase				:






			'''simple docstring'''

			__UpperCamelCase      :						List[str]					      = string.ascii_uppercase + string.digits
			# This cipher takes alphanumerics into account
			# i.e. a total of 36 characters

			# take x and return x % len(key_string)
			__UpperCamelCase      :						Any					      = numpy.vectorize(lambda _A  : x % 36  )

			__UpperCamelCase      :						Optional[Any]					      = numpy.vectorize(_A  )

			def __init__(       self :       str						,	snake_case_ :       numpy.ndarray      ):
							UpperCamelCase_:							str	=						self.modulus(snake_case_      )  # mod36 calc's on the encrypt key
							self.check_determinant()  # validate the determinant of the encryption key
							UpperCamelCase_:							Optional[Any]	=						encrypt_key.shape[0]

			def       lowerCAmelCase__	(       self :       Any						,	snake_case_ :       str      ):
							return self.key_string.index(snake_case_      )

			def       lowerCAmelCase__	(       self :       Any						,	snake_case_ :       int      ):
							return self.key_string[round(snake_case_      )]

			def       lowerCAmelCase__	(       self :       str      ):
							UpperCamelCase_:							List[str]	=						round(numpy.linalg.det(self.encrypt_key      )      )

							if det < 0:
											UpperCamelCase_:							Optional[Any]	=						det % len(self.key_string      )

							UpperCamelCase_:							str	=						len(self.key_string      )
							if greatest_common_divisor(snake_case_						,	len(self.key_string      )      ) != 1:
											UpperCamelCase_:							List[Any]	=						(
											    f'''determinant modular {req_l} of encryption key({det}) '''
											    f'''is not co prime w.r.t {req_l}.\nTry another key.'''
											)
											raise ValueError(snake_case_      )

			def       lowerCAmelCase__	(       self :       str						,	snake_case_ :       str      ):
							UpperCamelCase_:							Optional[int]	=						[char for char in text.upper() if char in self.key_string]

							UpperCamelCase_:							Tuple	=						chars[-1]
							while len(snake_case_      ) % self.break_key != 0:
											chars.append(snake_case_      )

							return "".join(snake_case_      )

			def       lowerCAmelCase__	(       self :       Dict						,	snake_case_ :       str      ):
							UpperCamelCase_:							str	=						self.process_text(text.upper()      )
							UpperCamelCase_:							Union[str, Any]	=						""""""

							for i in range(0						,	len(snake_case_      ) - self.break_key + 1						,	self.break_key      ):
											UpperCamelCase_:							Tuple	=						text[i : i + self.break_key]
											UpperCamelCase_:							str	=						[self.replace_letters(snake_case_      ) for char in batch]
											UpperCamelCase_:							Any	=						numpy.array([vec]      ).T
											UpperCamelCase_:							List[str]	=						self.modulus(self.encrypt_key.dot(snake_case_      )      ).T.tolist()[
											    0
											]
											UpperCamelCase_:							List[str]	=						"""""".join(
											    self.replace_digits(snake_case_      ) for num in batch_encrypted      )
											encrypted += encrypted_batch

							return encrypted

			def       lowerCAmelCase__	(       self :       Any      ):
							UpperCamelCase_:							str	=						round(numpy.linalg.det(self.encrypt_key      )      )

							if det < 0:
											UpperCamelCase_:							str	=						det % len(self.key_string      )
							UpperCamelCase_:							Any	=						None
							for i in range(len(self.key_string      )      ):
											if (det * i) % len(self.key_string      ) == 1:
															UpperCamelCase_:							Union[str, Any]	=						i
															break

							UpperCamelCase_:							List[Any]	=						(
							    det_inv
							    * numpy.linalg.det(self.encrypt_key      )
							    * numpy.linalg.inv(self.encrypt_key      )
							)

							return self.to_int(self.modulus(snake_case_      )      )




			def       lowerCAmelCase__	(       self :       Dict						,	snake_case_ :       str      ):
							UpperCamelCase_:							Union[str, Any]	=						self.make_decrypt_key()
							UpperCamelCase_:							List[Any]	=						self.process_text(text.upper()      )
							UpperCamelCase_:							Union[str, Any]	=						""""""

							for i in range(0						,	len(snake_case_      ) - self.break_key + 1						,	self.break_key      ):
											UpperCamelCase_:							Dict	=						text[i : i + self.break_key]
											UpperCamelCase_:							List[str]	=						[self.replace_letters(snake_case_      ) for char in batch]
											UpperCamelCase_:							Optional[int]	=						numpy.array([vec]      ).T
											UpperCamelCase_:							Optional[Any]	=						self.modulus(decrypt_key.dot(snake_case_      )      ).T.tolist()[0]
											UpperCamelCase_:							Union[str, Any]	=						"""""".join(
											    self.replace_digits(snake_case_      ) for num in batch_decrypted      )
											decrypted += decrypted_batch

							return decrypted







def        A__					(						)  ->	None:
				UpperCamelCase_:							Optional[Any]	=						int(input("""Enter the order of the encryption key: """						)						)
				UpperCamelCase_:							str	=						[]

				print("""Enter each row of the encryption key with space separated integers"""						)
				for _ in range(lowerCamelCase						):
								UpperCamelCase_:							Union[str, Any]	=						[int(lowerCamelCase						) for x in input().split()]
								hill_matrix.append(lowerCamelCase						)

				UpperCamelCase_:							List[Any]	=						HillCipher(numpy.array(lowerCamelCase						)						)

				print("""Would you like to encrypt or decrypt some text? (1 or 2)"""						)
				UpperCamelCase_:							Dict	=						input("""\n1. Encrypt\n2. Decrypt\n"""						)
				if option == "1":
								UpperCamelCase_:							Optional[Any]	=						input("""What text would you like to encrypt?: """						)
								print("""Your encrypted text is:"""						)
								print(hc.encrypt(lowerCamelCase						)						)
				elif option == "2":
								UpperCamelCase_:							Optional[int]	=						input("""What text would you like to decrypt?: """						)
								print("""Your decrypted text is:"""						)
								print(hc.decrypt(lowerCamelCase						)						)


if __name__ == "__main__":
							import doctest

							doctest.testmod()

							main()








import inspect
import os
import sys
import unittest

import accelerate
from accelerate.test_utils import execute_subprocess_async, require_tpu

class        _UpperCamelCase				(					unittest.TestCase  ):






			'''simple docstring'''

			def       lowerCAmelCase__	(       self :       Optional[int]      ):
							UpperCamelCase_:							List[Any]	=						inspect.getfile(accelerate.test_utils      )
							UpperCamelCase_:							List[str]	=						os.path.sep.join(mod_file.split(os.path.sep      )[:-1] + ["""scripts""", """test_script.py"""]      )
							UpperCamelCase_:							str	=						os.path.sep.join(inspect.getfile(self.__class__      ).split(os.path.sep      )[:-1]      )




			@require_tpu
			def       lowerCAmelCase__	(       self :       Optional[int]      ):
							UpperCamelCase_:							Any	=						f'''
            {self.test_dir}/xla_spawn.py
            --num_cores 8
            {self.test_file_path}
        '''.split()
							UpperCamelCase_:							Dict	=						[sys.executable] + distributed_args
							execute_subprocess_async(snake_case_						,	env=os.environ.copy()      )






