


from __future__ import annotations

from decimal import Decimal

from numpy import array



def     __lowerCAmelCase						(							snake_case	:    list[list[float]] )       ->							list[list[float]]:
   __lowerCamelCase:				Dict     =	Decimal

   # Check if the provided matrix has 2 rows and 2 columns
   # since this implementation only works for 2x2 matrices
   if len(snake_case ) == 2 and len(matrix[0] ) == 2 and len(matrix[1] ) == 2:
      # Calculate the determinant of the matrix
      __lowerCamelCase:				List[Any]     =	float(
          d(matrix[0][0] ) * d(matrix[1][1] ) - d(matrix[1][0] ) * d(matrix[0][1] ) )
      if determinant == 0:
         raise ValueError("""This matrix has no inverse.""" )

      # Creates a copy of the matrix with swapped positions of the elements
      __lowerCamelCase:				Optional[int]     =	[[0.0, 0.0], [0.0, 0.0]]
      __lowerCamelCase:				Tuple     =	matrix[1][1], matrix[0][0]
      __lowerCamelCase:				Tuple     =	-matrix[1][0], -matrix[0][1]

      # Calculate the inverse of the matrix
      return [
          [(float(d(snake_case ) ) / determinant) or 0.0 for n in row] for row in swapped_matrix
      ]
   elif (
       len(snake_case ) == 3
       and len(matrix[0] ) == 3
       and len(matrix[1] ) == 3
       and len(matrix[2] ) == 3
   ):
      # Calculate the determinant of the matrix using Sarrus rule
      __lowerCamelCase:				str     =	float(
          (
              (d(matrix[0][0] ) * d(matrix[1][1] ) * d(matrix[2][2] ))
              + (d(matrix[0][1] ) * d(matrix[1][2] ) * d(matrix[2][0] ))
              + (d(matrix[0][2] ) * d(matrix[1][0] ) * d(matrix[2][1] ))
          )
          - (
              (d(matrix[0][2] ) * d(matrix[1][1] ) * d(matrix[2][0] ))
              + (d(matrix[0][1] ) * d(matrix[1][0] ) * d(matrix[2][2] ))
              + (d(matrix[0][0] ) * d(matrix[1][2] ) * d(matrix[2][1] ))
          ) )
      if determinant == 0:
         raise ValueError("""This matrix has no inverse.""" )

      # Creating cofactor matrix
      __lowerCamelCase:				List[str]     =	[
          [d(0.0 ), d(0.0 ), d(0.0 )],
          [d(0.0 ), d(0.0 ), d(0.0 )],
          [d(0.0 ), d(0.0 ), d(0.0 )],
      ]
      __lowerCamelCase:				Optional[Any]     =	(d(matrix[1][1] ) * d(matrix[2][2] )) - (
          d(matrix[1][2] ) * d(matrix[2][1] )
      )
      __lowerCamelCase:				Any     =	-(
          (d(matrix[1][0] ) * d(matrix[2][2] )) - (d(matrix[1][2] ) * d(matrix[2][0] ))
      )
      __lowerCamelCase:				Optional[int]     =	(d(matrix[1][0] ) * d(matrix[2][1] )) - (
          d(matrix[1][1] ) * d(matrix[2][0] )
      )
      __lowerCamelCase:				Optional[Any]     =	-(
          (d(matrix[0][1] ) * d(matrix[2][2] )) - (d(matrix[0][2] ) * d(matrix[2][1] ))
      )
      __lowerCamelCase:				List[str]     =	(d(matrix[0][0] ) * d(matrix[2][2] )) - (
          d(matrix[0][2] ) * d(matrix[2][0] )
      )
      __lowerCamelCase:				Dict     =	-(
          (d(matrix[0][0] ) * d(matrix[2][1] )) - (d(matrix[0][1] ) * d(matrix[2][0] ))
      )
      __lowerCamelCase:				Any     =	(d(matrix[0][1] ) * d(matrix[1][2] )) - (
          d(matrix[0][2] ) * d(matrix[1][1] )
      )
      __lowerCamelCase:				Union[str, Any]     =	-(
          (d(matrix[0][0] ) * d(matrix[1][2] )) - (d(matrix[0][2] ) * d(matrix[1][0] ))
      )
      __lowerCamelCase:				List[Any]     =	(d(matrix[0][0] ) * d(matrix[1][1] )) - (
          d(matrix[0][1] ) * d(matrix[1][0] )
      )

      # Transpose the cofactor matrix (Adjoint matrix)
      __lowerCamelCase:				int     =	array(snake_case )
      for i in range(3 ):
         for j in range(3 ):
            __lowerCamelCase:				Any     =	cofactor_matrix[j][i]

        # Inverse of the matrix using the formula (1/determinant) * adjoint matrix
      __lowerCamelCase:				str     =	array(snake_case )
      for i in range(3 ):
         for j in range(3 ):
            inverse_matrix[i][j] /= d(snake_case )

        # Calculate the inverse of the matrix
      return [[float(d(snake_case ) ) or 0.0 for n in row] for row in inverse_matrix]
   raise ValueError("""Please provide a matrix of size 2x2 or 3x3.""" )






_A		:				Tuple									=       '''ABCDEFGHIJKLMNOPQRSTUVWXYZ'''



def     __lowerCAmelCase						(							)       ->							None:
   __lowerCamelCase:				Optional[int]     =	input("""Enter message: """ )
   __lowerCamelCase:				Dict     =	input("""Enter key [alphanumeric]: """ )
   __lowerCamelCase:				List[Any]     =	input("""Encrypt/Decrypt [e/d]: """ )

   if mode.lower().startswith("""e""" ):
      __lowerCamelCase:				Optional[int]     =	"""encrypt"""
      __lowerCamelCase:				Optional[int]     =	encrypt_message(snake_case						,      snake_case )
   elif mode.lower().startswith("""d""" ):
      __lowerCamelCase:				Union[str, Any]     =	"""decrypt"""
      __lowerCamelCase:				Optional[Any]     =	decrypt_message(snake_case						,      snake_case )

   print(f'\n{mode.title()}ed message:' )
   print(snake_case )



def     __lowerCAmelCase						(							snake_case	:    str						,      snake_case	:    str )       ->							str:
   return translate_message(snake_case						,      snake_case						,      """encrypt""" )



def     __lowerCAmelCase						(							snake_case	:    str						,      snake_case	:    str )       ->							str:
   return translate_message(snake_case						,      snake_case						,      """decrypt""" )



def     __lowerCAmelCase						(							snake_case	:    str						,      snake_case	:    str						,      snake_case	:    str )       ->							str:
   __lowerCamelCase:				Any     =	[]
   __lowerCamelCase:				Optional[int]     =	0
   __lowerCamelCase:				Any     =	key.upper()

   for symbol in message:
      __lowerCamelCase:				int     =	LETTERS.find(symbol.upper() )
      if num != -1:
         if mode == "encrypt":
            num += LETTERS.find(key[key_index] )
         elif mode == "decrypt":
            num -= LETTERS.find(key[key_index] )

         num %= len(snake_case )

         if symbol.isupper():
            translated.append(LETTERS[num] )
         elif symbol.islower():
            translated.append(LETTERS[num].lower() )

         key_index += 1
         if key_index == len(snake_case ):
            __lowerCamelCase:				Union[str, Any]     =	0
      else:
         translated.append(snake_case )
   return "".join(snake_case )


if __name__ == "__main__":
   main()


