Skip to content

Commit 56441f8

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent d0aa99b commit 56441f8

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

strings/wildcard_pattern_matching_fft.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
from numpy.fft import fft, ifft
33

4+
45
def preprocess_text_and_pattern(text: str, pattern: str) -> tuple[list[int], list[int]]:
56
"""Preprocesses text and pattern for pattern matching.
67
@@ -19,12 +20,14 @@ def preprocess_text_and_pattern(text: str, pattern: str) -> tuple[list[int], lis
1920
>>> preprocess_text_and_pattern("hello", "he*o")
2021
([3, 2, 4, 4, 5], [3, 2, 0, 5])
2122
"""
22-
23+
2324
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
2528

2629
# 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]
2831
text_int = [char_to_int[char] for char in text]
2932

3033
return text_int, pattern_int
@@ -44,7 +47,7 @@ def fft_convolution(input_seq_a: np.ndarray, input_seq_b: np.ndarray) -> np.ndar
4447
>>> fft_convolution(np.array([1, 2, 3]), np.array([0, 1, 0.5]))
4548
array([0. , 1. , 2.5, 3. , 1.5])
4649
"""
47-
50+
4851
n = len(input_seq_a) + len(input_seq_b) - 1
4952
A = fft(input_seq_a, n)
5053
B = fft(input_seq_b, n)
@@ -65,29 +68,30 @@ def compute_a_fft(text_int: list[int], pattern_int: list[int]) -> np.ndarray:
6568
>>> compute_a_fft([1, 2, 3, 1, 2, 3], [1, 2, 3, 0])
6669
array([...]) # Replace with the expected output based on your implementation
6770
"""
68-
71+
6972
n = len(text_int)
7073
m = len(pattern_int)
7174

7275
# Power transforms of the pattern and text based on the formula
7376
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])
7679

7780
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])
8083

8184
# Convolution to calculate the terms for A[i]
8285
sum1 = fft_convolution(p3[::-1], t1)
8386
sum2 = fft_convolution(p2[::-1], t2)
8487
sum3 = fft_convolution(p1[::-1], t3)
8588

8689
# 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]
8891

8992
return A
9093

94+
9195
# Main function to run the matching
9296
if __name__ == "__main__":
9397
# Example test case

0 commit comments

Comments
 (0)