






"""simple docstring"""
import string

import numpy

def 							snake_case (  A__      ,A__		):
			return b if a == 0 else greatest_common_divisor(b % a      ,A__		)







class      UpperCamelCase_			:
	__magic_name__       =							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)
	__magic_name__       =							numpy.vectorize(lambda __A	: x % 36	)

	__magic_name__       =							numpy.vectorize(__A	)





	def __init__( self   :					Union[str, Any]      ,      lowerCAmelCase_   :					numpy.ndarray		)		->     None:
				UpperCAmelCase_	:					Optional[int]     	=   self.modulus(lowerCAmelCase_		)  # mod36 calc's on the encrypt key
				self.check_determinant()  # validate the determinant of the encryption key
				UpperCAmelCase_	:					Optional[int]     	=   encrypt_key.shape[0]





	def 	_SCREAMING_SNAKE_CASE ( self   :					Dict      ,      lowerCAmelCase_   :					str		)		->     int:
				return self.key_string.index(lowerCAmelCase_		)





	def 	_SCREAMING_SNAKE_CASE ( self   :					Dict      ,      lowerCAmelCase_   :					int		)		->     str:
				return self.key_string[round(lowerCAmelCase_		)]





	def 	_SCREAMING_SNAKE_CASE ( self   :					Optional[Any]		)		->     None:
				UpperCAmelCase_	:					Union[str, Any]     	=   round(numpy.linalg.det(self.encrypt_key		)		)

				if det < 0:
							UpperCAmelCase_	:					Union[str, Any]     	=   det % len(self.key_string		)

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





	def 	_SCREAMING_SNAKE_CASE ( self   :					Tuple      ,      lowerCAmelCase_   :					str		)		->     str:
				UpperCAmelCase_	:					Dict     	=   [char for char in text.upper() if char in self.key_string]

				UpperCAmelCase_	:					List[Any]     	=   chars[-1]
				while len(lowerCAmelCase_		) % self.break_key != 0:
							chars.append(lowerCAmelCase_		)

				return "".join(lowerCAmelCase_		)





	def 	_SCREAMING_SNAKE_CASE ( self   :					str      ,      lowerCAmelCase_   :					str		)		->     str:
				UpperCAmelCase_	:					Optional[int]     	=   self.process_text(text.upper()		)
				UpperCAmelCase_	:					List[Any]     	=   ""

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

				return encrypted





	def 	_SCREAMING_SNAKE_CASE ( self   :					Tuple		)		->     numpy.ndarray:
				UpperCAmelCase_	:					str     	=   round(numpy.linalg.det(self.encrypt_key		)		)

				if det < 0:
							UpperCAmelCase_	:					Union[str, Any]     	=   det % len(self.key_string		)
				UpperCAmelCase_	:					Optional[int]     	=   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[str]     	=   (
				    det_inv
				    * numpy.linalg.det(self.encrypt_key		)
				    * numpy.linalg.inv(self.encrypt_key		)
				)

				return self.to_int(self.modulus(lowerCAmelCase_		)		)





	def 	_SCREAMING_SNAKE_CASE ( self   :					Tuple      ,      lowerCAmelCase_   :					str		)		->     str:
				UpperCAmelCase_	:					Union[str, Any]     	=   self.make_decrypt_key()
				UpperCAmelCase_	:					Optional[int]     	=   self.process_text(text.upper()		)
				UpperCAmelCase_	:					Optional[int]     	=   ""

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

				return decrypted





def 							snake_case (  ):
			UpperCAmelCase_	:					Tuple     	=   int(input("Enter the order of the encryption key: "		)		)
			UpperCAmelCase_	:					Optional[int]     	=   []

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

			UpperCAmelCase_	:					Optional[Any]     	=   HillCipher(numpy.array(A__		)		)

			print("Would you like to encrypt or decrypt some text? (1 or 2)"		)
			UpperCAmelCase_	:					List[str]     	=   input("\n1. Encrypt\n2. Decrypt\n"		)
			if option == "1":
						UpperCAmelCase_	:					Tuple     	=   input("What text would you like to encrypt?: "		)
						print("Your encrypted text is:"		)
						print(hc.encrypt(A__		)		)
			elif option == "2":
						UpperCAmelCase_	:					str     	=   input("What text would you like to decrypt?: "		)
						print("Your decrypted text is:"		)
						print(hc.decrypt(A__		)		)


if __name__ == "__main__":
			import doctest

			doctest.testmod()

			main()







import warnings

from ...utils import is_sklearn_available, requires_backends


if is_sklearn_available():
     from scipy.stats import pearsonr, spearmanr
     from sklearn.metrics import fa_score, matthews_corrcoef


snake_case							:       List[str]										=	(
    '''This metric will be removed from the library soon, metrics should be handled with the 🤗 Evaluate '''
    '''library. You can have a look at this example script for pointers: '''
    '''https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py'''
)
def 							__lowerCamelCase		(					UpperCAmelCase_		:						List[str]					,    UpperCAmelCase_		:						Optional[Any]		):




      """simple docstring"""





      warnings.warn(UpperCAmelCase_					,    UpperCAmelCase_		)
      requires_backends(UpperCAmelCase_					,    '''sklearn'''		)
      return (preds == labels).mean()
def 							__lowerCamelCase		(					UpperCAmelCase_		:						str					,    UpperCAmelCase_		:						int		):




      """simple docstring"""





      warnings.warn(UpperCAmelCase_					,    UpperCAmelCase_		)
      requires_backends(UpperCAmelCase_					,    '''sklearn'''		)
      a		:Dict									=			simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)
      a		:Any									=			fa_score(y_true=UpperCAmelCase_					,    y_pred=UpperCAmelCase_		)
      return {
          "acc": acc,
          "f1": fa,
          "acc_and_f1": (acc + fa) / 2,
      }
def 							__lowerCamelCase		(					UpperCAmelCase_		:						Dict					,    UpperCAmelCase_		:						List[Any]		):




      """simple docstring"""





      warnings.warn(UpperCAmelCase_					,    UpperCAmelCase_		)
      requires_backends(UpperCAmelCase_					,    '''sklearn'''		)
      a		:str									=			pearsonr(UpperCAmelCase_					,    UpperCAmelCase_		)[0]
      a		:Union[str, Any]									=			spearmanr(UpperCAmelCase_					,    UpperCAmelCase_		)[0]
      return {
          "pearson": pearson_corr,
          "spearmanr": spearman_corr,
          "corr": (pearson_corr + spearman_corr) / 2,
      }
def 							__lowerCamelCase		(					UpperCAmelCase_		:						int					,    UpperCAmelCase_		:						int					,    UpperCAmelCase_		:						Dict		):




      """simple docstring"""





      warnings.warn(UpperCAmelCase_					,    UpperCAmelCase_		)
      requires_backends(UpperCAmelCase_					,    '''sklearn'''		)
      assert len(UpperCAmelCase_		) == len(UpperCAmelCase_		), F'''Predictions and labels have mismatched lengths {len(UpperCAmelCase_		)} and {len(UpperCAmelCase_		)}'''
      if task_name == "cola":
            return {"mcc": matthews_corrcoef(UpperCAmelCase_					,    UpperCAmelCase_		)}
      elif task_name == "sst-2":
            return {"acc": simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)}
      elif task_name == "mrpc":
            return acc_and_fa(UpperCAmelCase_					,    UpperCAmelCase_		)
      elif task_name == "sts-b":
            return pearson_and_spearman(UpperCAmelCase_					,    UpperCAmelCase_		)
      elif task_name == "qqp":
            return acc_and_fa(UpperCAmelCase_					,    UpperCAmelCase_		)
      elif task_name == "mnli":
            return {"mnli/acc": simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)}
      elif task_name == "mnli-mm":
            return {"mnli-mm/acc": simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)}
      elif task_name == "qnli":
            return {"acc": simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)}
      elif task_name == "rte":
            return {"acc": simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)}
      elif task_name == "wnli":
            return {"acc": simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)}
      elif task_name == "hans":
            return {"acc": simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)}
      else:
            raise KeyError(UpperCAmelCase_		)






def 							__lowerCamelCase		(					UpperCAmelCase_		:						List[str]					,    UpperCAmelCase_		:						int					,    UpperCAmelCase_		:						List[Any]		):




      """simple docstring"""





      warnings.warn(UpperCAmelCase_					,    UpperCAmelCase_		)
      requires_backends(UpperCAmelCase_					,    '''sklearn'''		)
      if len(UpperCAmelCase_		) != len(UpperCAmelCase_		):
            raise ValueError(F'''Predictions and labels have mismatched lengths {len(UpperCAmelCase_		)} and {len(UpperCAmelCase_		)}'''		)
      if task_name == "xnli":
            return {"acc": simple_accuracy(UpperCAmelCase_					,    UpperCAmelCase_		)}
      else:
            raise KeyError(UpperCAmelCase_		)





