
import string

import numpy




def 				a						(							snake_case__:					int						,   snake_case__:					int					):


				'''simple docstring'''
				return b if a == 0 else greatest_common_divisor(b % a						,   snake_case__					)
class  lowercase__:



	"""simple docstring"""




	a			:Tuple         =	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)
	a			:str         =	numpy.vectorize(lambda UpperCAmelCase							: x % 36							)

	a			:int         =	numpy.vectorize(UpperCAmelCase							)
	def __init__(       self  :			List[str]       ,							SCREAMING_SNAKE_CASE_  :			numpy.ndarray       )       ->     None:
					lowercase_       =					self.modulus(SCREAMING_SNAKE_CASE_       )  # mod36 calc's on the encrypt key
					self.check_determinant()  # validate the determinant of the encryption key
					lowercase_       =					encrypt_key.shape[0]
	def 						_lowercase  (       self  :			Any       ,							SCREAMING_SNAKE_CASE_  :			str       )       ->     int:
					return self.key_string.index(SCREAMING_SNAKE_CASE_       )
	def 						_lowercase  (       self  :			Any       ,							SCREAMING_SNAKE_CASE_  :			int       )       ->     str:
					return self.key_string[round(SCREAMING_SNAKE_CASE_       )]
	def 						_lowercase  (       self  :			Optional[Any]       )       ->     None:
					lowercase_       =					round(numpy.linalg.det(self.encrypt_key       )       )

					if det < 0:
									lowercase_       =					det % len(self.key_string       )

					lowercase_       =					len(self.key_string       )
					if greatest_common_divisor(SCREAMING_SNAKE_CASE_       ,							len(self.key_string       )       ) != 1:
									lowercase_       =					(
									    f'''determinant modular {req_l} of encryption key({det}) '''
									    f'''is not co prime w.r.t {req_l}.\nTry another key.'''
									)
									raise ValueError(SCREAMING_SNAKE_CASE_       )
	def 						_lowercase  (       self  :			List[str]       ,							SCREAMING_SNAKE_CASE_  :			str       )       ->     str:
					lowercase_       =					[char for char in text.upper() if char in self.key_string]

					lowercase_       =					chars[-1]
					while len(SCREAMING_SNAKE_CASE_       ) % self.break_key != 0:
									chars.append(SCREAMING_SNAKE_CASE_       )

					return "".join(SCREAMING_SNAKE_CASE_       )
	def 						_lowercase  (       self  :			List[str]       ,							SCREAMING_SNAKE_CASE_  :			str       )       ->     str:
					lowercase_       =					self.process_text(text.upper()       )
					lowercase_       =					''''''

					for i in range(0       ,							len(SCREAMING_SNAKE_CASE_       ) - self.break_key + 1       ,							self.break_key       ):
									lowercase_       =					text[i : i + self.break_key]
									lowercase_       =					[self.replace_letters(SCREAMING_SNAKE_CASE_       ) for char in batch]
									lowercase_       =					numpy.array([vec]       ).T
									lowercase_       =					self.modulus(self.encrypt_key.dot(SCREAMING_SNAKE_CASE_       )       ).T.tolist()[
									    0
									]
									lowercase_       =					''''''.join(
									    self.replace_digits(SCREAMING_SNAKE_CASE_       ) for num in batch_encrypted       )
									encrypted += encrypted_batch

					return encrypted
	def 						_lowercase  (       self  :			Union[str, Any]       )       ->     numpy.ndarray:
					lowercase_       =					round(numpy.linalg.det(self.encrypt_key       )       )

					if det < 0:
									lowercase_       =					det % len(self.key_string       )
					lowercase_       =					None
					for i in range(len(self.key_string       )       ):
									if (det * i) % len(self.key_string       ) == 1:
													lowercase_       =					i
													break

					lowercase_       =					(
					    det_inv
					    * numpy.linalg.det(self.encrypt_key       )
					    * numpy.linalg.inv(self.encrypt_key       )
					)

					return self.to_int(self.modulus(SCREAMING_SNAKE_CASE_       )       )




	def 						_lowercase  (       self  :			Optional[Any]       ,							SCREAMING_SNAKE_CASE_  :			str       )       ->     str:
					lowercase_       =					self.make_decrypt_key()
					lowercase_       =					self.process_text(text.upper()       )
					lowercase_       =					''''''

					for i in range(0       ,							len(SCREAMING_SNAKE_CASE_       ) - self.break_key + 1       ,							self.break_key       ):
									lowercase_       =					text[i : i + self.break_key]
									lowercase_       =					[self.replace_letters(SCREAMING_SNAKE_CASE_       ) for char in batch]
									lowercase_       =					numpy.array([vec]       ).T
									lowercase_       =					self.modulus(decrypt_key.dot(SCREAMING_SNAKE_CASE_       )       ).T.tolist()[0]
									lowercase_       =					''''''.join(
									    self.replace_digits(SCREAMING_SNAKE_CASE_       ) for num in batch_decrypted       )
									decrypted += decrypted_batch

					return decrypted




def 				a						(							):


				'''simple docstring'''
				lowercase_       =					int(input('''Enter the order of the encryption key: '''					)					)
				lowercase_       =					[]

				print('''Enter each row of the encryption key with space separated integers'''					)
				for _ in range(snake_case__					):
								lowercase_       =					[int(snake_case__					) for x in input().split()]
								hill_matrix.append(snake_case__					)

				lowercase_       =					HillCipher(numpy.array(snake_case__					)					)

				print('''Would you like to encrypt or decrypt some text? (1 or 2)'''					)
				lowercase_       =					input('''\n1. Encrypt\n2. Decrypt\n'''					)
				if option == "1":
								lowercase_       =					input('''What text would you like to encrypt?: '''					)
								print('''Your encrypted text is:'''					)
								print(hc.encrypt(snake_case__					)					)
				elif option == "2":
								lowercase_       =					input('''What text would you like to decrypt?: '''					)
								print('''Your decrypted text is:'''					)
								print(hc.decrypt(snake_case__					)					)


if __name__ == "__main__":
						import doctest

						doctest.testmod()

						main()










from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


lowerCamelCase__ :   str					=							{
    'configuration_mask2former': [
        'MASK2FORMER_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'Mask2FormerConfig',
    ],
}

try:
   if not is_vision_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   lowerCamelCase__ :   int					=							['Mask2FormerImageProcessor']

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   lowerCamelCase__ :   Dict					=							[
       'MASK2FORMER_PRETRAINED_MODEL_ARCHIVE_LIST',
       'Mask2FormerForUniversalSegmentation',
       'Mask2FormerModel',
       'Mask2FormerPreTrainedModel',
   ]

if TYPE_CHECKING:
   from .configuration_maskaformer import MASK2FORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, MaskaFormerConfig

   try:
      if not is_vision_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .image_processing_maskaformer import MaskaFormerImageProcessor

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_maskaformer import (
          MASK2FORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
          MaskaFormerForUniversalSegmentation,
          MaskaFormerModel,
          MaskaFormerPreTrainedModel,
      )


else:
   import sys

   lowerCamelCase__ :   Tuple					=							_LazyModule(__name__, globals()['__file__'], _import_structure)