@@ -12,14 +12,7 @@ def preprocess_text_and_pattern(text: str, pattern: str) -> tuple[list[int], lis
12
12
A tuple containing:
13
13
- A list of integers representing the text characters.
14
14
- 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])
21
15
"""
22
-
23
16
unique_chars = set (text + pattern )
24
17
char_to_int = {char : i + 1 for i , char in enumerate (unique_chars )} # Unique non-zero integers
25
18
@@ -29,27 +22,20 @@ def preprocess_text_and_pattern(text: str, pattern: str) -> tuple[list[int], lis
29
22
30
23
return text_int , pattern_int
31
24
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 :
34
26
"""Performs convolution using the Fast Fourier Transform (FFT).
35
27
36
28
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.
39
31
40
32
Returns:
41
33
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])
46
34
"""
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 ))
53
39
54
40
def compute_a_fft (text_int : list [int ], pattern_int : list [int ]) -> np .ndarray :
55
41
"""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:
60
46
61
47
Returns:
62
48
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
67
49
"""
68
-
69
50
n = len (text_int )
70
51
m = len (pattern_int )
71
52
72
53
# Power transforms of the pattern and text based on the formula
73
54
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 ])
76
57
77
58
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 ])
80
61
81
62
# Convolution to calculate the terms for A[i]
82
63
sum1 = fft_convolution (p3 [::- 1 ], t1 )
83
64
sum2 = fft_convolution (p2 [::- 1 ], t2 )
84
65
sum3 = fft_convolution (p1 [::- 1 ], t3 )
85
66
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 ]
88
69
89
- return A
70
+ return a
90
71
91
72
# Main function to run the matching
92
73
if __name__ == "__main__" :
@@ -100,9 +81,9 @@ def compute_a_fft(text_int: list[int], pattern_int: list[int]) -> np.ndarray:
100
81
print ("Preprocessed pattern:" , pattern_int )
101
82
102
83
# 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 )
105
86
106
87
# 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 )]
108
89
print ("Pattern matches at indices:" , matches )
0 commit comments