



import string

import numpy






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


class 				_a						:





							'''simple docstring'''

							A				:Any =       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				:int =       numpy.vectorize(lambda SCREAMING_SNAKE_CASE			: x % 36			)

							A				:Optional[Any] =       numpy.vectorize(SCREAMING_SNAKE_CASE			)



							def __init__(							self ,					__UpperCAmelCase				):







											"""simple docstring"""



											a__	:							Optional[int]      						= self.modulus(__UpperCAmelCase				)  # mod36 calc's on the encrypt key
											self.check_determinant()  # validate the determinant of the encryption key
											a__	:							Union[str, Any]      						= encrypt_key.shape[0]



							def 		_A	(							self ,					__UpperCAmelCase				):







											"""simple docstring"""



											return self.key_string.index(__UpperCAmelCase				)



							def 		_A	(							self ,					__UpperCAmelCase				):







											"""simple docstring"""



											return self.key_string[round(__UpperCAmelCase				)]



							def 		_A	(							self				):







											"""simple docstring"""



											a__	:							Optional[Any]      						= round(numpy.linalg.det(self.encrypt_key				)				)

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

											a__	:							List[str]      						= len(self.key_string				)
											if greatest_common_divisor(__UpperCAmelCase ,					len(self.key_string				)				) != 1:
															a__	:							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(__UpperCAmelCase				)



							def 		_A	(							self ,					__UpperCAmelCase				):







											"""simple docstring"""



											a__	:							Any      						= [char for char in text.upper() if char in self.key_string]

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

											return "".join(__UpperCAmelCase				)



							def 		_A	(							self ,					__UpperCAmelCase				):







											"""simple docstring"""



											a__	:							Optional[Any]      						= self.process_text(text.upper()				)
											a__	:							int      						= ""

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

											return encrypted



							def 		_A	(							self				):







											"""simple docstring"""



											a__	:							int      						= round(numpy.linalg.det(self.encrypt_key				)				)

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

											a__	:							str      						= (
											    det_inv
											    * numpy.linalg.det(self.encrypt_key				)
											    * numpy.linalg.inv(self.encrypt_key				)
											)

											return self.to_int(self.modulus(__UpperCAmelCase				)				)




							def 		_A	(							self ,					__UpperCAmelCase				):







											"""simple docstring"""



											a__	:							List[Any]      						= self.make_decrypt_key()
											a__	:							List[str]      						= self.process_text(text.upper()				)
											a__	:							int      						= ""

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

											return decrypted






def     SCREAMING_SNAKE_CASE(      )	->						None:
				a__	:							int      						= int(input("Enter the order of the encryption key: " ) )
				a__	:							Dict      						= []

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

				a__	:							int      						= HillCipher(numpy.array(__UpperCamelCase ) )

				print("Would you like to encrypt or decrypt some text? (1 or 2)" )
				a__	:							Dict      						= input("\n1. Encrypt\n2. Decrypt\n" )
				if option == "1":
								a__	:							List[Any]      						= input("What text would you like to encrypt?: " )
								print("Your encrypted text is:" )
								print(hc.encrypt(__UpperCamelCase ) )
				elif option == "2":
								a__	:							Tuple      						= 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 unittest

import numpy as np
import requests

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
						import torch

						from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
						lowerCamelCase		       =      False

if is_vision_available():
						from PIL import Image

						from transformers import PixaStructImageProcessor


class 				_a						(       unittest.TestCase			):





							'''simple docstring'''

							def __init__(							self ,					__UpperCAmelCase ,					__UpperCAmelCase=7 ,					__UpperCAmelCase=3 ,					__UpperCAmelCase=18 ,					__UpperCAmelCase=30 ,					__UpperCAmelCase=400 ,					__UpperCAmelCase=None ,					__UpperCAmelCase=True ,					__UpperCAmelCase=True ,					__UpperCAmelCase=None ,					):







											"""simple docstring"""



											a__	:							str      						= size if size is not None else {"height": 20, "width": 20}
											a__	:							Optional[int]      						= parent
											a__	:							Union[str, Any]      						= batch_size
											a__	:							List[Any]      						= num_channels
											a__	:							Union[str, Any]      						= image_size
											a__	:							List[str]      						= min_resolution
											a__	:							int      						= max_resolution
											a__	:							int      						= size
											a__	:							List[str]      						= do_normalize
											a__	:							Any      						= do_convert_rgb
											a__	:							Optional[Any]      						= [512, 1024, 2048, 4096]
											a__	:							Any      						= patch_size if patch_size is not None else {"height": 16, "width": 16}



							def 		_A	(							self				):







											"""simple docstring"""



											return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb}




							def 		_A	(							self				):







											"""simple docstring"""



											a__	:							str      						= "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg"
											a__	:							Union[str, Any]      						= Image.open(requests.get(__UpperCAmelCase ,					stream=__UpperCAmelCase				).raw				).convert("RGB"				)
											return raw_image





@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11       ,  reason="`Pix2StructImageProcessor` requires `torch>=1.11.0`."       ,  )
@require_torch
@require_vision
class 				_a						(       SCREAMING_SNAKE_CASE       ,  unittest.TestCase			):





							'''simple docstring'''

							A				:List[str] =       PixaStructImageProcessor if is_vision_available() else None



							def 		_A	(							self				):







											"""simple docstring"""



											a__	:							Any      						= PixaStructImageProcessingTester(self				)



							@property
							def 		_A	(							self				):







											"""simple docstring"""



											return self.image_processor_tester.prepare_image_processor_dict()



							def 		_A	(							self				):







											"""simple docstring"""



											a__	:							Union[str, Any]      						= self.image_processing_class(**self.image_processor_dict				)
											self.assertTrue(hasattr(__UpperCAmelCase ,					"do_normalize"				)				)
											self.assertTrue(hasattr(__UpperCAmelCase ,					"do_convert_rgb"				)				)



							def 		_A	(							self				):







											"""simple docstring"""



											a__	:							Tuple      						= self.image_processor_tester.prepare_dummy_image()

											a__	:							Any      						= self.image_processing_class(**self.image_processor_dict				)
											a__	:							List[str]      						= 2048

											a__	:							Union[str, Any]      						= image_processor(__UpperCAmelCase ,					return_tensors="pt" ,					max_patches=__UpperCAmelCase				)
											self.assertTrue(torch.allclose(inputs.flattened_patches.mean() ,					torch.tensor(0.0_6_0_6				) ,					atol=1E-3 ,					rtol=1E-3				)				)



							def 		_A	(							self				):







											"""simple docstring"""



											a__	:							int      						= self.image_processing_class(**self.image_processor_dict				)
											# create random PIL images
											a__	:							str      						= prepare_image_inputs(self.image_processor_tester ,					equal_resolution=__UpperCAmelCase				)
											for image in image_inputs:
															self.assertIsInstance(__UpperCAmelCase ,					Image.Image				)

											# Test not batched input
											a__	:							List[Any]      						= (
											    (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
											    * self.image_processor_tester.num_channels
											) + 2

											for max_patch in self.image_processor_tester.max_patches:
															# Test not batched input
															a__	:							Optional[Any]      						= image_processor(
															    image_inputs[0] ,					return_tensors="pt" ,					max_patches=__UpperCAmelCase				).flattened_patches
															self.assertEqual(
															    encoded_images.shape ,					(1, max_patch, expected_hidden_dim) ,					)

															# Test batched
															a__	:							Optional[Any]      						= image_processor(
															    __UpperCAmelCase ,					return_tensors="pt" ,					max_patches=__UpperCAmelCase				).flattened_patches
															self.assertEqual(
															    encoded_images.shape ,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) ,					)



							def 		_A	(							self				):







											"""simple docstring"""



											a__	:							Union[str, Any]      						= self.image_processing_class(**self.image_processor_dict				)
											# create random PIL images
											a__	:							Any      						= prepare_image_inputs(self.image_processor_tester ,					equal_resolution=__UpperCAmelCase				)
											for image in image_inputs:
															self.assertIsInstance(__UpperCAmelCase ,					Image.Image				)

											# Test not batched input
											a__	:							Optional[int]      						= (
											    (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
											    * self.image_processor_tester.num_channels
											) + 2

											a__	:							int      						= True

											for max_patch in self.image_processor_tester.max_patches:
															# Test not batched input
															with self.assertRaises(__UpperCAmelCase				):
																			a__	:							List[str]      						= image_processor(
																			    image_inputs[0] ,					return_tensors="pt" ,					max_patches=__UpperCAmelCase				).flattened_patches

															a__	:							Tuple      						= "Hello"

															a__	:							Union[str, Any]      						= image_processor(
															    image_inputs[0] ,					return_tensors="pt" ,					max_patches=__UpperCAmelCase ,					header_text=__UpperCAmelCase				).flattened_patches
															self.assertEqual(
															    encoded_images.shape ,					(1, max_patch, expected_hidden_dim) ,					)

															# Test batched
															a__	:							Optional[int]      						= image_processor(
															    __UpperCAmelCase ,					return_tensors="pt" ,					max_patches=__UpperCAmelCase ,					header_text=__UpperCAmelCase				).flattened_patches
															self.assertEqual(
															    encoded_images.shape ,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) ,					)



							def 		_A	(							self				):







											"""simple docstring"""



											a__	:							str      						= self.image_processing_class(**self.image_processor_dict				)
											# create random numpy tensors
											a__	:							Tuple      						= prepare_image_inputs(self.image_processor_tester ,					equal_resolution=__UpperCAmelCase ,					numpify=__UpperCAmelCase				)
											for image in image_inputs:
															self.assertIsInstance(__UpperCAmelCase ,					np.ndarray				)

											a__	:							List[str]      						= (
											    (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
											    * self.image_processor_tester.num_channels
											) + 2

											for max_patch in self.image_processor_tester.max_patches:
															# Test not batched input
															a__	:							Any      						= image_processor(
															    image_inputs[0] ,					return_tensors="pt" ,					max_patches=__UpperCAmelCase				).flattened_patches
															self.assertEqual(
															    encoded_images.shape ,					(1, max_patch, expected_hidden_dim) ,					)

															# Test batched
															a__	:							str      						= image_processor(
															    __UpperCAmelCase ,					return_tensors="pt" ,					max_patches=__UpperCAmelCase				).flattened_patches
															self.assertEqual(
															    encoded_images.shape ,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) ,					)




							def 		_A	(							self				):







											"""simple docstring"""



											a__	:							List[Any]      						= self.image_processing_class(**self.image_processor_dict				)
											# create random PyTorch tensors
											a__	:							Any      						= prepare_image_inputs(self.image_processor_tester ,					equal_resolution=__UpperCAmelCase ,					torchify=__UpperCAmelCase				)
											for image in image_inputs:
															self.assertIsInstance(__UpperCAmelCase ,					torch.Tensor				)

											# Test not batched input
											a__	:							Optional[Any]      						= (
											    (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
											    * self.image_processor_tester.num_channels
											) + 2

											for max_patch in self.image_processor_tester.max_patches:
															# Test not batched input
															a__	:							str      						= image_processor(
															    image_inputs[0] ,					return_tensors="pt" ,					max_patches=__UpperCAmelCase				).flattened_patches
															self.assertEqual(
															    encoded_images.shape ,					(1, max_patch, expected_hidden_dim) ,					)

															# Test batched
															a__	:							Optional[int]      						= image_processor(
															    __UpperCAmelCase ,					return_tensors="pt" ,					max_patches=__UpperCAmelCase				).flattened_patches
															self.assertEqual(
															    encoded_images.shape ,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) ,					)





@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11       ,  reason="`Pix2StructImageProcessor` requires `torch>=1.11.0`."       ,  )
@require_torch
@require_vision
class 				_a						(       SCREAMING_SNAKE_CASE       ,  unittest.TestCase			):





							'''simple docstring'''

							A				:List[str] =       PixaStructImageProcessor if is_vision_available() else None



							def 		_A	(							self				):







											"""simple docstring"""



											a__	:							Dict      						= PixaStructImageProcessingTester(self ,					num_channels=4				)
											a__	:							Tuple      						= 3



							@property
							def 		_A	(							self				):







											"""simple docstring"""



											return self.image_processor_tester.prepare_image_processor_dict()



							def 		_A	(							self				):







											"""simple docstring"""



											a__	:							Optional[int]      						= self.image_processing_class(**self.image_processor_dict				)
											self.assertTrue(hasattr(__UpperCAmelCase ,					"do_normalize"				)				)
											self.assertTrue(hasattr(__UpperCAmelCase ,					"do_convert_rgb"				)				)




							def 		_A	(							self				):







											"""simple docstring"""



											a__	:							Dict      						= self.image_processing_class(**self.image_processor_dict				)
											# create random PIL images
											a__	:							Union[str, Any]      						= prepare_image_inputs(self.image_processor_tester ,					equal_resolution=__UpperCAmelCase				)
											for image in image_inputs:
															self.assertIsInstance(__UpperCAmelCase ,					Image.Image				)

											# Test not batched input
											a__	:							str      						= (
											    (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
											    * (self.image_processor_tester.num_channels - 1)
											) + 2

											for max_patch in self.image_processor_tester.max_patches:
															# Test not batched input
															a__	:							List[str]      						= image_processor(
															    image_inputs[0] ,					return_tensors="pt" ,					max_patches=__UpperCAmelCase				).flattened_patches
															self.assertEqual(
															    encoded_images.shape ,					(1, max_patch, expected_hidden_dim) ,					)

															# Test batched
															a__	:							int      						= image_processor(
															    __UpperCAmelCase ,					return_tensors="pt" ,					max_patches=__UpperCAmelCase				).flattened_patches
															self.assertEqual(
															    encoded_images.shape ,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) ,					)


