import string

import numpy





def 							A			(  _UpperCAmelCase       :     str							,     _UpperCAmelCase       :     Any					)       ->				int:







			'''simple docstring'''
			return b if a == 0 else greatest_common_divisor(b % a							,     _UpperCAmelCase					)







class     __lowerCAmelCase					:
			UpperCamelCase           =  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           =  numpy.vectorize(lambda A		: x % 3_6		)

			UpperCamelCase           =  numpy.vectorize(SCREAMING_SNAKE_CASE__		)



			def __init__(		self     :						Dict				,	A     :						List[Any])     ->					Union[str, Any]:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.modulus(snake_case__)  # mod36 calc's on the encrypt key
						self.check_determinant()  # validate the determinant of the encryption key
						_UpperCAmelCase	 =     encrypt_key.shape[0]



			def 					_lowerCamelCase (		self     :						Union[str, Any]				,	A     :						str)     ->					List[Any]:



						"""simple docstring"""






						return self.key_string.index(snake_case__)



			def 					_lowerCamelCase (		self     :						Tuple				,	A     :						Optional[int])     ->					str:



						"""simple docstring"""






						return self.key_string[round(snake_case__)]



			def 					_lowerCamelCase (		self     :						int)     ->					Optional[int]:



						"""simple docstring"""






						_UpperCAmelCase	 =     round(numpy.linalg.det(self.encrypt_key))

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

						_UpperCAmelCase	 =     len(self.key_string)
						if greatest_common_divisor(snake_case__				,	len(self.key_string)) != 1:
									_UpperCAmelCase	 =     (
									    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     :						Tuple				,	A     :						Optional[int])     ->					List[Any]:



						"""simple docstring"""






						_UpperCAmelCase	 =     [char for char in text.upper() if char in self.key_string]

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

						return "".join(snake_case__)



			def 					_lowerCamelCase (		self     :						Any				,	A     :						Tuple)     ->					Union[str, Any]:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.process_text(text.upper())
						_UpperCAmelCase	 =     ""

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

						return encrypted



			def 					_lowerCamelCase (		self     :						List[str])     ->					Optional[Any]:



						"""simple docstring"""






						_UpperCAmelCase	 =     round(numpy.linalg.det(self.encrypt_key))

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

						_UpperCAmelCase	 =     (
						    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     :						Union[str, Any]				,	A     :						Optional[int])     ->					Union[str, Any]:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.make_decrypt_key()
						_UpperCAmelCase	 =     self.process_text(text.upper())
						_UpperCAmelCase	 =     ""

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

						return decrypted





def 							A			(  )       ->				None:







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

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

			_UpperCAmelCase	 =     HillCipher(numpy.array(_UpperCAmelCase					)					)

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


if __name__ == "__main__":
							import doctest

							doctest.testmod()

							main()





import json
import os
import unittest

from transformers import MgpstrTokenizer
from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin







@require_tokenizers
class     __lowerCAmelCase					(    A       ,		unittest.TestCase		):
			UpperCamelCase           =  MgpstrTokenizer
			UpperCamelCase           =  False
			UpperCamelCase           =  {}
			UpperCamelCase           =  False



			def 					_lowerCamelCase (		self     :						int)     ->					List[Any]:



						"""simple docstring"""






						super().setUp()

						# fmt: off
						_UpperCAmelCase	 =     ['[GO]', '[s]', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
						# fmt: on
						_UpperCAmelCase	 =     dict(zip(A				,	range(len(A))))

						_UpperCAmelCase	 =     os.path.join(self.tmpdirname				,	VOCAB_FILES_NAMES['vocab_file'])
						with open(self.vocab_file				,	'w'				,	encoding='utf-8') as fp:
									fp.write(json.dumps(A) + '\n')



			def 					_lowerCamelCase (		self     :						Dict				,	**A     :						List[Any])     ->					Optional[Any]:



						"""simple docstring"""






						return MgpstrTokenizer.from_pretrained(self.tmpdirname				,	**A)



			def 					_lowerCamelCase (		self     :						List[str]				,	A     :						Optional[int])     ->					Dict:



						"""simple docstring"""






						_UpperCAmelCase	 =     'tester'
						_UpperCAmelCase	 =     'tester'
						return input_text, output_text



			@unittest.skip('MGP-STR always lower cases letters.')
			def 					_lowerCamelCase (		self     :						Optional[Any])     ->					int:



						"""simple docstring"""






						pass



			def 					_lowerCamelCase (		self     :						Optional[int])     ->					Optional[int]:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.get_tokenizers(do_lower_case=A)
						for tokenizer in tokenizers:
									with self.subTest(F"{tokenizer.__class__.__name__}"):
												_UpperCAmelCase	 =     '[SPECIAL_TOKEN]'

												tokenizer.add_special_tokens({'cls_token': special_token})
												_UpperCAmelCase	 =     tokenizer.encode([special_token]				,	add_special_tokens=A)
												self.assertEqual(len(A)				,	1)

												_UpperCAmelCase	 =     tokenizer.decode(A				,	skip_special_tokens=A)
												self.assertTrue(special_token not in decoded)



			def 					_lowerCamelCase (		self     :						Any)     ->					str:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.get_tokenizers()
						for tokenizer in tokenizers:
									with self.subTest(F"{tokenizer.__class__.__name__}"):
												_UpperCAmelCase ,  _UpperCAmelCase	 =     self.get_input_output_texts(A)

												_UpperCAmelCase	 =     tokenizer.tokenize(A)
												_UpperCAmelCase	 =     tokenizer.convert_tokens_to_ids(A)
												_UpperCAmelCase	 =     tokenizer.encode(A				,	add_special_tokens=A)
												self.assertListEqual(A				,	A)

												_UpperCAmelCase	 =     tokenizer.convert_ids_to_tokens(A)
												self.assertNotEqual(len(A)				,	0)
												_UpperCAmelCase	 =     tokenizer.decode(A)
												self.assertIsInstance(A				,	A)

												self.assertEqual(text_a.replace(' '				,	'')				,	A)



			@unittest.skip('MGP-STR tokenizer only handles one sequence.')
			def 					_lowerCamelCase (		self     :						str)     ->					List[str]:



						"""simple docstring"""






						pass



			@unittest.skip('inputs cannot be pretokenized in MgpstrTokenizer')
			def 					_lowerCamelCase (		self     :						Dict)     ->					Tuple:



						"""simple docstring"""






						pass
