



import string

import numpy
def 							UpperCAmelCase__		(				_SCREAMING_SNAKE_CASE   :	int     ,				_SCREAMING_SNAKE_CASE   :	int				)->int:
      return b if a == 0 else greatest_common_divisor(b % a     ,				_SCREAMING_SNAKE_CASE				)





class 		UpperCAmelCase						:
   SCREAMING_SNAKE_CASE__							=     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)
   SCREAMING_SNAKE_CASE__							=     numpy.vectorize(lambda snake_case_				: x % 3_6				)

   SCREAMING_SNAKE_CASE__							=     numpy.vectorize(snake_case_				)





   def __init__(	self       ,     _lowerCAmelCase			):
         _lowerCAmelCase 			=  self.modulus(_lowerCAmelCase			)  # mod36 calc's on the encrypt key
         self.check_determinant()  # validate the determinant of the encryption key
         _lowerCAmelCase 			=  encrypt_key.shape[0]





   def 	__lowerCAmelCase							(	self       ,     _lowerCAmelCase			):
         return self.key_string.index(_lowerCAmelCase			)





   def 	__lowerCAmelCase							(	self       ,     _lowerCAmelCase			):
         return self.key_string[round(_lowerCAmelCase			)]





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  round(numpy.linalg.det(self.encrypt_key			)			)

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

         _lowerCAmelCase 			=  len(self.key_string			)
         if greatest_common_divisor(_lowerCAmelCase       ,     len(self.key_string			)			) != 1:
               _lowerCAmelCase 			=  (
                   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 	__lowerCAmelCase							(	self       ,     _lowerCAmelCase			):
         _lowerCAmelCase 			=  [char for char in text.upper() if char in self.key_string]

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

         return "".join(_lowerCAmelCase			)





   def 	__lowerCAmelCase							(	self       ,     _lowerCAmelCase			):
         _lowerCAmelCase 			=  self.process_text(text.upper()			)
         _lowerCAmelCase 			=  ''''''

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

         return encrypted





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  round(numpy.linalg.det(self.encrypt_key			)			)

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

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

         return self.to_int(self.modulus(_lowerCAmelCase			)			)





   def 	__lowerCAmelCase							(	self       ,     _lowerCAmelCase			):
         _lowerCAmelCase 			=  self.make_decrypt_key()
         _lowerCAmelCase 			=  self.process_text(text.upper()			)
         _lowerCAmelCase 			=  ''''''

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

         return decrypted






def 							UpperCAmelCase__		(				)->None:
      _lowerCAmelCase 			=  int(input('''Enter the order of the encryption key: '''				)				)
      _lowerCAmelCase 			=  []

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

      _lowerCAmelCase 			=  HillCipher(numpy.array(_SCREAMING_SNAKE_CASE				)				)

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


if __name__ == "__main__":
    import doctest

    doctest.testmod()

    main()




import unittest

import numpy as np

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_video_inputs


if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

    from transformers import VivitImageProcessor





class 		UpperCAmelCase						(						unittest.TestCase				):





   def __init__(	self       ,     _lowerCAmelCase       ,     _lowerCAmelCase=7       ,     _lowerCAmelCase=3       ,     _lowerCAmelCase=10       ,     _lowerCAmelCase=18       ,     _lowerCAmelCase=30       ,     _lowerCAmelCase=400       ,     _lowerCAmelCase=True       ,     _lowerCAmelCase=None       ,     _lowerCAmelCase=True       ,     _lowerCAmelCase=[0.5, 0.5, 0.5]       ,     _lowerCAmelCase=[0.5, 0.5, 0.5]       ,     _lowerCAmelCase=None       ,     ):
         _lowerCAmelCase 			=  size if size is not None else {'''shortest_edge''': 18}
         _lowerCAmelCase 			=  crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}

         _lowerCAmelCase 			=  parent
         _lowerCAmelCase 			=  batch_size
         _lowerCAmelCase 			=  num_channels
         _lowerCAmelCase 			=  num_frames
         _lowerCAmelCase 			=  image_size
         _lowerCAmelCase 			=  min_resolution
         _lowerCAmelCase 			=  max_resolution
         _lowerCAmelCase 			=  do_resize
         _lowerCAmelCase 			=  size
         _lowerCAmelCase 			=  do_normalize
         _lowerCAmelCase 			=  image_mean
         _lowerCAmelCase 			=  image_std
         _lowerCAmelCase 			=  crop_size





   def 	__lowerCAmelCase							(	self			):
         return {
             "image_mean": self.image_mean,
             "image_std": self.image_std,
             "do_normalize": self.do_normalize,
             "do_resize": self.do_resize,
             "size": self.size,
             "crop_size": self.crop_size,
         }






@require_torch
@require_vision
class 		UpperCAmelCase						(						snake_case_       ,unittest.TestCase				):
   SCREAMING_SNAKE_CASE__							=     VivitImageProcessor if is_vision_available() else None





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  VivitImageProcessingTester(self			)





   @property
   def 	__lowerCAmelCase							(	self			):
         return self.image_processor_tester.prepare_image_processor_dict()





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  self.image_processing_class(**self.image_processor_dict			)
         self.assertTrue(hasattr(_lowerCAmelCase       ,     '''image_mean'''			)			)
         self.assertTrue(hasattr(_lowerCAmelCase       ,     '''image_std'''			)			)
         self.assertTrue(hasattr(_lowerCAmelCase       ,     '''do_normalize'''			)			)
         self.assertTrue(hasattr(_lowerCAmelCase       ,     '''do_resize'''			)			)
         self.assertTrue(hasattr(_lowerCAmelCase       ,     '''do_center_crop'''			)			)
         self.assertTrue(hasattr(_lowerCAmelCase       ,     '''size'''			)			)





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  self.image_processing_class.from_dict(self.image_processor_dict			)
         self.assertEqual(image_processor.size       ,     {'''shortest_edge''': 18}			)
         self.assertEqual(image_processor.crop_size       ,     {'''height''': 18, '''width''': 18}			)

         _lowerCAmelCase 			=  self.image_processing_class.from_dict(self.image_processor_dict       ,     size=42       ,     crop_size=84			)
         self.assertEqual(image_processor.size       ,     {'''shortest_edge''': 42}			)
         self.assertEqual(image_processor.crop_size       ,     {'''height''': 84, '''width''': 84}			)





   def 	__lowerCAmelCase							(	self			):
         # Initialize image_processing
         _lowerCAmelCase 			=  self.image_processing_class(**self.image_processor_dict			)
         # create random PIL videos
         _lowerCAmelCase 			=  prepare_video_inputs(self.image_processor_tester       ,     equal_resolution=_lowerCAmelCase			)
         for video in video_inputs:
               self.assertIsInstance(_lowerCAmelCase       ,     _lowerCAmelCase			)
               self.assertIsInstance(video[0]       ,     Image.Image			)

         # Test not batched input
         _lowerCAmelCase 			=  image_processing(video_inputs[0]       ,     return_tensors='''pt'''			).pixel_values
         self.assertEqual(
             encoded_videos.shape       ,     (
                 1,
                 self.image_processor_tester.num_frames,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )       ,     )

         # Test batched
         _lowerCAmelCase 			=  image_processing(_lowerCAmelCase       ,     return_tensors='''pt'''			).pixel_values
         self.assertEqual(
             encoded_videos.shape       ,     (
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_frames,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )       ,     )





   def 	__lowerCAmelCase							(	self			):
         # Initialize image_processing
         _lowerCAmelCase 			=  self.image_processing_class(**self.image_processor_dict			)
         # create random numpy tensors
         _lowerCAmelCase 			=  prepare_video_inputs(self.image_processor_tester       ,     equal_resolution=_lowerCAmelCase       ,     numpify=_lowerCAmelCase			)
         for video in video_inputs:
               self.assertIsInstance(_lowerCAmelCase       ,     _lowerCAmelCase			)
               self.assertIsInstance(video[0]       ,     np.ndarray			)

         # Test not batched input
         _lowerCAmelCase 			=  image_processing(video_inputs[0]       ,     return_tensors='''pt'''			).pixel_values
         self.assertEqual(
             encoded_videos.shape       ,     (
                 1,
                 self.image_processor_tester.num_frames,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )       ,     )

         # Test batched
         _lowerCAmelCase 			=  image_processing(_lowerCAmelCase       ,     return_tensors='''pt'''			).pixel_values
         self.assertEqual(
             encoded_videos.shape       ,     (
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_frames,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )       ,     )





   def 	__lowerCAmelCase							(	self			):
         # Initialize image_processing
         _lowerCAmelCase 			=  self.image_processing_class(**self.image_processor_dict			)
         # create random PyTorch tensors
         _lowerCAmelCase 			=  prepare_video_inputs(self.image_processor_tester       ,     equal_resolution=_lowerCAmelCase       ,     torchify=_lowerCAmelCase			)
         for video in video_inputs:
               self.assertIsInstance(_lowerCAmelCase       ,     _lowerCAmelCase			)
               self.assertIsInstance(video[0]       ,     torch.Tensor			)

         # Test not batched input
         _lowerCAmelCase 			=  image_processing(video_inputs[0]       ,     return_tensors='''pt'''			).pixel_values
         self.assertEqual(
             encoded_videos.shape       ,     (
                 1,
                 self.image_processor_tester.num_frames,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )       ,     )

         # Test batched
         _lowerCAmelCase 			=  image_processing(_lowerCAmelCase       ,     return_tensors='''pt'''			).pixel_values
         self.assertEqual(
             encoded_videos.shape       ,     (
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_frames,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )       ,     )