



'''simple docstring'''





import string

import numpy

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






class 					lowerCamelCase_    :
  """simple docstring"""




  a_												=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_												=numpy.vectorize(lambda SCREAMING_SNAKE_CASE_     : x % 36     )

  a_												=numpy.vectorize(SCREAMING_SNAKE_CASE_     )




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




  def 						_lowercase     ( self      :	str			,	_a      :	str   )					->					int:
    return self.key_string.index(_a   )




  def 						_lowercase     ( self      :	int			,	_a      :	int   )					->					str:
    return self.key_string[round(_a   )]




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

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

    __lowerCamelCase       :       Tuple           = len(self.key_string   )
    if greatest_common_divisor(_a			,	len(self.key_string   )   ) != 1:
      __lowerCamelCase       :       Union[str, Any]           = (
          f'determinant modular {req_l} of encryption key({det}) '
          f'is not co prime w.r.t {req_l}.\nTry another key.'
      )
      raise ValueError(_a   )




  def 						_lowercase     ( self      :	Optional[int]			,	_a      :	str   )					->					str:
    __lowerCamelCase       :       Optional[int]           = [char for char in text.upper() if char in self.key_string]

    __lowerCamelCase       :       List[str]           = chars[-1]
    while len(_a   ) % self.break_key != 0:
      chars.append(_a   )

    return "".join(_a   )




  def 						_lowercase     ( self      :	Optional[Any]			,	_a      :	str   )					->					str:
    __lowerCamelCase       :       Tuple           = self.process_text(text.upper()   )
    __lowerCamelCase       :       Dict           = ''

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

    return encrypted




  def 						_lowercase     ( self      :	Any   )					->					numpy.ndarray:
    __lowerCamelCase       :       Dict           = round(numpy.linalg.det(self.encrypt_key   )   )

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

    __lowerCamelCase       :       Dict           = (
        det_inv
        * numpy.linalg.det(self.encrypt_key   )
        * numpy.linalg.inv(self.encrypt_key   )
    )

    return self.to_int(self.modulus(_a   )   )




  def 						_lowercase     ( self      :	Optional[Any]			,	_a      :	str   )					->					str:
    __lowerCamelCase       :       int           = self.make_decrypt_key()
    __lowerCamelCase       :       Tuple           = self.process_text(text.upper()   )
    __lowerCamelCase       :       Union[str, Any]           = ''

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

    return decrypted




def        a_     (							)      ->      None:
  __lowerCamelCase       :       List[Any]           = int(input('Enter the order of the encryption key: '       )       )
  __lowerCamelCase       :       Tuple           = []

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

  __lowerCamelCase       :       List[str]           = HillCipher(numpy.array(_lowerCAmelCase       )       )

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


if __name__ == "__main__":
   import doctest

   doctest.testmod()

   main()











'''simple docstring'''





import inspect
import warnings
from typing import Any, Dict, Optional, Union

from packaging import version

def        a_     (							*_lowerCAmelCase       ,_lowerCAmelCase = None       ,_lowerCAmelCase=True       ,_lowerCAmelCase=2       )      ->      List[str]:
  from .. import __version__

  __lowerCamelCase       :       Any           = take_from
  __lowerCamelCase       :       Optional[int]           = ()
  if not isinstance(args[0]       ,_lowerCAmelCase       ):
    __lowerCamelCase       :       Optional[Any]           = (args,)

  for attribute, version_name, message in args:
    if version.parse(version.parse(_lowerCAmelCase       ).base_version       ) >= version.parse(_lowerCAmelCase       ):
      raise ValueError(
          F'The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers\''
          F' version {__version__} is >= {version_name}'       )

    __lowerCamelCase       :       Union[str, Any]           = None
    if isinstance(_lowerCAmelCase       ,_lowerCAmelCase       ) and attribute in deprecated_kwargs:
      values += (deprecated_kwargs.pop(_lowerCAmelCase       ),)
      __lowerCamelCase       :       Optional[Any]           = F'The `{attribute}` argument is deprecated and will be removed in version {version_name}.'
    elif hasattr(_lowerCAmelCase       ,_lowerCAmelCase       ):
      values += (getattr(_lowerCAmelCase       ,_lowerCAmelCase       ),)
      __lowerCamelCase       :       List[str]           = F'The `{attribute}` attribute is deprecated and will be removed in version {version_name}.'
    elif deprecated_kwargs is None:
      __lowerCamelCase       :       Optional[Any]           = F'`{attribute}` is deprecated and will be removed in version {version_name}.'

    if warning is not None:
      __lowerCamelCase       :       Optional[int]           = warning + ' ' if standard_warn else ''
      warnings.warn(warning + message       ,_lowerCAmelCase       ,stacklevel=_lowerCAmelCase       )

  if isinstance(_lowerCAmelCase       ,_lowerCAmelCase       ) and len(_lowerCAmelCase       ) > 0:
    __lowerCamelCase       :       Optional[Any]           = inspect.getouterframes(inspect.currentframe()       )[1]
    __lowerCamelCase       :       List[str]           = call_frame.filename
    __lowerCamelCase       :       int           = call_frame.lineno
    __lowerCamelCase       :       Union[str, Any]           = call_frame.function
    __lowerCamelCase       ,__lowerCamelCase       :       Union[str, Any]           = next(iter(deprecated_kwargs.items()       )       )
    raise TypeError(F'{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`'       )

  if len(_lowerCAmelCase       ) == 0:
    return
  elif len(_lowerCAmelCase       ) == 1:
    return values[0]
  return values






