

import string

import numpy







def 	_snake_case		(    SCREAMING_SNAKE_CASE		,							SCREAMING_SNAKE_CASE				)       ->      int:

     """simple docstring"""







     return b if a == 0 else greatest_common_divisor(b % a		,							SCREAMING_SNAKE_CASE				)





class A__    :



 """simple docstring"""







 _lowercase     :					Tuple      =						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)
 _lowercase     :					Union[str, Any]      =						numpy.vectorize(lambda A : x % 36 )

 _lowercase     :					List[str]      =						numpy.vectorize(A )

 def __init__(		self :	Optional[Any]     ,						A_ :	numpy.ndarray       ):






      '''simple docstring'''




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

 def    __magic_name__       (		self :	Optional[int]     ,						A_ :	str       ):






      '''simple docstring'''




      return self.key_string.index(A_       )

 def    __magic_name__       (		self :	str     ,						A_ :	int       ):






      '''simple docstring'''




      return self.key_string[round(A_       )]

 def    __magic_name__       (		self :	str       ):






      '''simple docstring'''




      _lowerCAmelCase   :					List[str]        =    round(numpy.linalg.det(self.encrypt_key       )       )

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

      _lowerCAmelCase   :					Tuple        =    len(self.key_string       )
      if greatest_common_divisor(A_     ,						len(self.key_string       )       ) != 1:
           _lowerCAmelCase   :					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(A_       )

 def    __magic_name__       (		self :	Any     ,						A_ :	str       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Optional[Any]        =    [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    __magic_name__       (		self :	int     ,						A_ :	str       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Optional[int]        =    self.process_text(text.upper()       )
      _lowerCAmelCase   :					List[str]        =    ""

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

      return encrypted

 def    __magic_name__       (		self :	List[Any]       ):






      '''simple docstring'''




      _lowerCAmelCase   :					List[str]        =    round(numpy.linalg.det(self.encrypt_key       )       )

      if det < 0:
           _lowerCAmelCase   :					Optional[Any]        =    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[str]        =    i
                break

      _lowerCAmelCase   :					List[Any]        =    (
          det_inv
          * numpy.linalg.det(self.encrypt_key       )
          * numpy.linalg.inv(self.encrypt_key       )
      )

      return self.to_int(self.modulus(A_       )       )


 def    __magic_name__       (		self :	str     ,						A_ :	str       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Tuple        =    self.make_decrypt_key()
      _lowerCAmelCase   :					Tuple        =    self.process_text(text.upper()       )
      _lowerCAmelCase   :					int        =    ""

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

      return decrypted







def 	_snake_case		(    )       ->      None:

     """simple docstring"""







     _lowerCAmelCase   :					List[Any]        =    int(input("Enter the order of the encryption key: "				)				)
     _lowerCAmelCase   :					int        =    []

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

     _lowerCAmelCase   :					Tuple        =    HillCipher(numpy.array(SCREAMING_SNAKE_CASE				)				)

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









# Author: OMKAR PATHAK, Nwachukwu Chidiebere

# Use a Python dictionary to construct the graph.
from __future__ import annotations

from pprint import pformat
from typing import Generic, TypeVar

__UpperCAmelCase    				=     TypeVar('T')





class A__    ( Generic[T] ):



 """simple docstring"""







 def __init__(		self :	Optional[int]     ,						A_ :	bool = True       ):






      '''simple docstring'''




      _lowerCAmelCase   :					dict[T, list[T]]        =    {}  # dictionary of lists
      _lowerCAmelCase   :					List[Any]        =    directed

 def    __magic_name__       (		self :	Any     ,						A_ :	T     ,						A_ :	T       ):






      '''simple docstring'''




      if not self.directed:  # For undirected graphs
           # if both source vertex and destination vertex are both present in the
           # adjacency list, add destination vertex to source vertex list of adjacent
           # vertices and add source vertex to destination vertex list of adjacent
           # vertices.
           if source_vertex in self.adj_list and destination_vertex in self.adj_list:
                self.adj_list[source_vertex].append(A_       )
                self.adj_list[destination_vertex].append(A_       )
           # if only source vertex is present in adjacency list, add destination vertex
           # to source vertex list of adjacent vertices, then create a new vertex with
           # destination vertex as key and assign a list containing the source vertex
           # as it's first adjacent vertex.
           elif source_vertex in self.adj_list:
                self.adj_list[source_vertex].append(A_       )
                _lowerCAmelCase   :					List[Any]        =    [source_vertex]
           # if only destination vertex is present in adjacency list, add source vertex
           # to destination vertex list of adjacent vertices, then create a new vertex
           # with source vertex as key and assign a list containing the source vertex
           # as it's first adjacent vertex.
           elif destination_vertex in self.adj_list:
                self.adj_list[destination_vertex].append(A_       )
                _lowerCAmelCase   :					List[str]        =    [destination_vertex]
           # if both source vertex and destination vertex are not present in adjacency
           # list, create a new vertex with source vertex as key and assign a list
           # containing the destination vertex as it's first adjacent vertex also
           # create a new vertex with destination vertex as key and assign a list
           # containing the source vertex as it's first adjacent vertex.
           else:
                _lowerCAmelCase   :					List[str]        =    [destination_vertex]
                _lowerCAmelCase   :					Dict        =    [source_vertex]
      else:  # For directed graphs
           # if both source vertex and destination vertex are present in adjacency
           # list, add destination vertex to source vertex list of adjacent vertices.
           if source_vertex in self.adj_list and destination_vertex in self.adj_list:
                self.adj_list[source_vertex].append(A_       )
           # if only source vertex is present in adjacency list, add destination
           # vertex to source vertex list of adjacent vertices and create a new vertex
           # with destination vertex as key, which has no adjacent vertex
           elif source_vertex in self.adj_list:
                self.adj_list[source_vertex].append(A_       )
                _lowerCAmelCase   :					Tuple        =    []
           # if only destination vertex is present in adjacency list, create a new
           # vertex with source vertex as key and assign a list containing destination
           # vertex as first adjacent vertex
           elif destination_vertex in self.adj_list:
                _lowerCAmelCase   :					Dict        =    [destination_vertex]
           # if both source vertex and destination vertex are not present in adjacency
           # list, create a new vertex with source vertex as key and a list containing
           # destination vertex as it's first adjacent vertex. Then create a new vertex
           # with destination vertex as key, which has no adjacent vertex
           else:
                _lowerCAmelCase   :					Any        =    [destination_vertex]
                _lowerCAmelCase   :					Union[str, Any]        =    []

      return self


 def __repr__(		self :	List[str]       ):






      '''simple docstring'''




      return pformat(self.adj_list       )






