1
1
import numpy as np
2
2
from numpy .fft import fft , ifft
3
3
4
+
4
5
def preprocess_text_and_pattern (text : str , pattern : str ) -> tuple [list [int ], list [int ]]:
5
6
"""Preprocesses text and pattern for pattern matching.
6
7
@@ -19,12 +20,14 @@ def preprocess_text_and_pattern(text: str, pattern: str) -> tuple[list[int], lis
19
20
>>> preprocess_text_and_pattern("hello", "he*o")
20
21
([3, 2, 4, 4, 5], [3, 2, 0, 5])
21
22
"""
22
-
23
+
23
24
unique_chars = set (text + pattern )
24
- char_to_int = {char : i + 1 for i , char in enumerate (unique_chars )} # Unique non-zero integers
25
+ char_to_int = {
26
+ char : i + 1 for i , char in enumerate (unique_chars )
27
+ } # Unique non-zero integers
25
28
26
29
# Replace pattern '*' with 0, other characters with their unique integers
27
- pattern_int = [char_to_int [char ] if char != '*' else 0 for char in pattern ]
30
+ pattern_int = [char_to_int [char ] if char != "*" else 0 for char in pattern ]
28
31
text_int = [char_to_int [char ] for char in text ]
29
32
30
33
return text_int , pattern_int
@@ -44,7 +47,7 @@ def fft_convolution(input_seq_a: np.ndarray, input_seq_b: np.ndarray) -> np.ndar
44
47
>>> fft_convolution(np.array([1, 2, 3]), np.array([0, 1, 0.5]))
45
48
array([0. , 1. , 2.5, 3. , 1.5])
46
49
"""
47
-
50
+
48
51
n = len (input_seq_a ) + len (input_seq_b ) - 1
49
52
A = fft (input_seq_a , n )
50
53
B = fft (input_seq_b , n )
@@ -65,29 +68,30 @@ def compute_a_fft(text_int: list[int], pattern_int: list[int]) -> np.ndarray:
65
68
>>> compute_a_fft([1, 2, 3, 1, 2, 3], [1, 2, 3, 0])
66
69
array([...]) # Replace with the expected output based on your implementation
67
70
"""
68
-
71
+
69
72
n = len (text_int )
70
73
m = len (pattern_int )
71
74
72
75
# Power transforms of the pattern and text based on the formula
73
76
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 ])
77
+ p2 = np .array ([p ** 2 for p in pattern_int ])
78
+ p3 = np .array ([p ** 3 for p in pattern_int ])
76
79
77
80
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 ])
81
+ t2 = np .array ([t ** 2 for t in text_int ])
82
+ t3 = np .array ([t ** 3 for t in text_int ])
80
83
81
84
# Convolution to calculate the terms for A[i]
82
85
sum1 = fft_convolution (p3 [::- 1 ], t1 )
83
86
sum2 = fft_convolution (p2 [::- 1 ], t2 )
84
87
sum3 = fft_convolution (p1 [::- 1 ], t3 )
85
88
86
89
# Calculate A[i] using the convolution results
87
- A = sum1 [:n - m + 1 ] - 2 * sum2 [:n - m + 1 ] + sum3 [:n - m + 1 ]
90
+ A = sum1 [: n - m + 1 ] - 2 * sum2 [: n - m + 1 ] + sum3 [: n - m + 1 ]
88
91
89
92
return A
90
93
94
+
91
95
# Main function to run the matching
92
96
if __name__ == "__main__" :
93
97
# Example test case
0 commit comments