
"""simple docstring"""





from __future__ import annotations

from decimal import Decimal

from numpy import array







def 	__a     (		__lowerCamelCase  ):
   UpperCAmelCase_					:					Any			     =    Decimal

   # Check if the provided matrix has 2 rows and 2 columns
   # since this implementation only works for 2x2 matrices
   if len(__lowerCamelCase  ) == 2 and len(matrix[0]  ) == 2 and len(matrix[1]  ) == 2:
      # Calculate the determinant of the matrix
      UpperCAmelCase_					:					Optional[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
      UpperCAmelCase_					:					Union[str, Any]			     =    [[0.0, 0.0], [0.0, 0.0]]
      UpperCAmelCase_     ,   UpperCAmelCase_					:					Any			     =    matrix[1][1], matrix[0][0]
      UpperCAmelCase_     ,   UpperCAmelCase_					:					Dict			     =    -matrix[1][0], -matrix[0][1]

      # Calculate the inverse of the matrix
      return [
          [(float(d(__lowerCamelCase  )  ) / determinant) or 0.0 for n in row] for row in swapped_matrix
      ]
   elif (
       len(__lowerCamelCase  ) == 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
      UpperCAmelCase_					:					int			     =    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
      UpperCAmelCase_					:					Optional[Any]			     =    [
          [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  )],
      ]
      UpperCAmelCase_					:					int			     =    (d(matrix[1][1]  ) * d(matrix[2][2]  )) - (
          d(matrix[1][2]  ) * d(matrix[2][1]  )
      )
      UpperCAmelCase_					:					int			     =    -(
          (d(matrix[1][0]  ) * d(matrix[2][2]  )) - (d(matrix[1][2]  ) * d(matrix[2][0]  ))
      )
      UpperCAmelCase_					:					int			     =    (d(matrix[1][0]  ) * d(matrix[2][1]  )) - (
          d(matrix[1][1]  ) * d(matrix[2][0]  )
      )
      UpperCAmelCase_					:					Dict			     =    -(
          (d(matrix[0][1]  ) * d(matrix[2][2]  )) - (d(matrix[0][2]  ) * d(matrix[2][1]  ))
      )
      UpperCAmelCase_					:					List[str]			     =    (d(matrix[0][0]  ) * d(matrix[2][2]  )) - (
          d(matrix[0][2]  ) * d(matrix[2][0]  )
      )
      UpperCAmelCase_					:					int			     =    -(
          (d(matrix[0][0]  ) * d(matrix[2][1]  )) - (d(matrix[0][1]  ) * d(matrix[2][0]  ))
      )
      UpperCAmelCase_					:					List[Any]			     =    (d(matrix[0][1]  ) * d(matrix[1][2]  )) - (
          d(matrix[0][2]  ) * d(matrix[1][1]  )
      )
      UpperCAmelCase_					:					int			     =    -(
          (d(matrix[0][0]  ) * d(matrix[1][2]  )) - (d(matrix[0][2]  ) * d(matrix[1][0]  ))
      )
      UpperCAmelCase_					:					str			     =    (d(matrix[0][0]  ) * d(matrix[1][1]  )) - (
          d(matrix[0][1]  ) * d(matrix[1][0]  )
      )

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

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

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




SCREAMING_SNAKE_CASE							:Any							 =				256
# Modulus to hash a string
SCREAMING_SNAKE_CASE							:Union[str, Any]							 =				100_0003


def 						UpperCAmelCase			(	a_		,   a_						)		->	bool:







				"""simple docstring"""



				__A   			= len(a_						)
				__A   			= len(a_						)
				if p_len > t_len:
								return False

				__A   			= 0
				__A   			= 0
				__A   			= 1

				# Calculating the hash of pattern and substring of text
				for i in range(a_						):
								__A   			= (ord(pattern[i]						) + p_hash * alphabet_size) % modulus
								__A   			= (ord(text[i]						) + text_hash * alphabet_size) % modulus
								if i == p_len - 1:
												continue
								__A   			= (modulus_power * alphabet_size) % modulus

				for i in range(0		,   t_len - p_len + 1						):
								if text_hash == p_hash and text[i : i + p_len] == pattern:
												return True
								if i == t_len - p_len:
												continue
								# Calculate the https://en.wikipedia.org/wiki/Rolling_hash
								__A   			= (
								    (text_hash - ord(text[i]						) * modulus_power) * alphabet_size
								    + ord(text[i + p_len]						)
								) % modulus
				return False


def 						UpperCAmelCase			(	)		->	None:







				"""simple docstring"""



				__A   			= "abc1abc12"
				__A   			= "alskfjaldsabc1abc1abc12k23adsfabcabc"
				__A   			= "alskfjaldsk23adsfabcabc"
				assert rabin_karp(a_		,   a_						) and not rabin_karp(a_		,   a_						)

				# Test 2)
				__A   			= "ABABX"
				__A   			= "ABABZABABYABABX"
				assert rabin_karp(a_		,   a_						)

				# Test 3)
				__A   			= "AAAB"
				__A   			= "ABAAAAAB"
				assert rabin_karp(a_		,   a_						)

				# Test 4)
				__A   			= "abcdabcy"
				__A   			= "abcxabcdabxabcdabcdabcy"
				assert rabin_karp(a_		,   a_						)

				# Test 5)
				__A   			= "Lü"
				__A   			= "Lüsai"
				assert rabin_karp(a_		,   a_						)
				__A   			= "Lue"
				assert not rabin_karp(a_		,   a_						)
				print("Success."						)


if __name__ == "__main__":
					test_rabin_karp()
