@@ -12,14 +12,7 @@ def preprocess_text_and_pattern(text: str, pattern: str) -> tuple[list[int], lis
1212 A tuple containing:
1313 - A list of integers representing the text characters.
1414 - A list of integers representing the pattern characters, with 0 for wildcards.
15-
16- Examples:
17- >>> preprocess_text_and_pattern("abcabc", "abc*")
18- ([1, 2, 3, 1, 2, 3], [1, 2, 3, 0])
19- >>> preprocess_text_and_pattern("hello", "he*o")
20- ([3, 2, 4, 4, 5], [3, 2, 0, 5])
2115 """
22-
2316 unique_chars = set (text + pattern )
2417 char_to_int = {char : i + 1 for i , char in enumerate (unique_chars )} # Unique non-zero integers
2518
@@ -29,27 +22,20 @@ def preprocess_text_and_pattern(text: str, pattern: str) -> tuple[list[int], lis
2922
3023 return text_int , pattern_int
3124
32-
33- def fft_convolution (input_seq_a : np .ndarray , input_seq_b : np .ndarray ) -> np .ndarray :
25+ def fft_convolution (a : list [int ], b : list [int ]) -> np .ndarray :
3426 """Performs convolution using the Fast Fourier Transform (FFT).
3527
3628 Args:
37- input_seq_a : The first sequence (1D numpy array) .
38- input_seq_b : The second sequence (1D numpy array) .
29+ a : The first sequence.
30+ b : The second sequence.
3931
4032 Returns:
4133 The convolution of the two sequences.
42-
43- Examples:
44- >>> fft_convolution(np.array([1, 2, 3]), np.array([0, 1, 0.5]))
45- array([0. , 1. , 2.5, 3. , 1.5])
4634 """
47-
48- n = len (input_seq_a ) + len (input_seq_b ) - 1
49- A = fft (input_seq_a , n )
50- B = fft (input_seq_b , n )
51- return np .real (ifft (A * B ))
52-
35+ n = len (a ) + len (b ) - 1
36+ a_fft = fft (a , n )
37+ b_fft = fft (b , n )
38+ return np .real (ifft (a_fft * b_fft ))
5339
5440def compute_a_fft (text_int : list [int ], pattern_int : list [int ]) -> np .ndarray :
5541 """Computes the A array for the pattern matching algorithm.
@@ -60,33 +46,28 @@ def compute_a_fft(text_int: list[int], pattern_int: list[int]) -> np.ndarray:
6046
6147 Returns:
6248 The A array.
63-
64- Examples:
65- >>> compute_a_fft([1, 2, 3, 1, 2, 3], [1, 2, 3, 0])
66- array([...]) # Replace with the expected output based on your implementation
6749 """
68-
6950 n = len (text_int )
7051 m = len (pattern_int )
7152
7253 # Power transforms of the pattern and text based on the formula
7354 p1 = np .array (pattern_int )
74- p2 = np .array ([p ** 2 for p in pattern_int ])
75- p3 = np .array ([p ** 3 for p in pattern_int ])
55+ p2 = np .array ([p ** 2 for p in pattern_int ])
56+ p3 = np .array ([p ** 3 for p in pattern_int ])
7657
7758 t1 = np .array (text_int )
78- t2 = np .array ([t ** 2 for t in text_int ])
79- t3 = np .array ([t ** 3 for t in text_int ])
59+ t2 = np .array ([t ** 2 for t in text_int ])
60+ t3 = np .array ([t ** 3 for t in text_int ])
8061
8162 # Convolution to calculate the terms for A[i]
8263 sum1 = fft_convolution (p3 [::- 1 ], t1 )
8364 sum2 = fft_convolution (p2 [::- 1 ], t2 )
8465 sum3 = fft_convolution (p1 [::- 1 ], t3 )
8566
86- # Calculate A [i] using the convolution results
87- A = sum1 [:n - m + 1 ] - 2 * sum2 [:n - m + 1 ] + sum3 [:n - m + 1 ]
67+ # Calculate a [i] using the convolution results
68+ a = sum1 [:n - m + 1 ] - 2 * sum2 [:n - m + 1 ] + sum3 [:n - m + 1 ]
8869
89- return A
70+ return a
9071
9172# Main function to run the matching
9273if __name__ == "__main__" :
@@ -100,9 +81,9 @@ def compute_a_fft(text_int: list[int], pattern_int: list[int]) -> np.ndarray:
10081 print ("Preprocessed pattern:" , pattern_int )
10182
10283 # Compute A array
103- A = compute_a_fft (text_int , pattern_int )
104- print ("A array:" , A )
84+ a = compute_a_fft (text_int , pattern_int )
85+ print ("A array:" , a )
10586
10687 # Find matches
107- matches = [i for i in range (len (A )) if np .isclose (A [i ], 0 )]
88+ matches = [i for i in range (len (a )) if np .isclose (a [i ], 0 )]
10889 print ("Pattern matches at indices:" , matches )
0 commit comments