From e7c0701228da078e3ee115d31f655ac98899f958 Mon Sep 17 00:00:00 2001 From: Avaya Aggarwal <119044997+OnePunchMonk@users.noreply.github.com> Date: Sat, 12 Oct 2024 21:49:34 +0530 Subject: [PATCH 1/8] Add wildcard pattern matching algorithm using FFT --- strings/wildcard_pattern_matching_fft.py | 96 ++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 strings/wildcard_pattern_matching_fft.py diff --git a/strings/wildcard_pattern_matching_fft.py b/strings/wildcard_pattern_matching_fft.py new file mode 100644 index 000000000000..fb9e309a1e5f --- /dev/null +++ b/strings/wildcard_pattern_matching_fft.py @@ -0,0 +1,96 @@ +import numpy as np +from numpy.fft import fft, ifft + +def preprocess_text_and_pattern(text, pattern): + """Preprocesses text and pattern for pattern matching. + + Args: + text: The input text string. + pattern: The input pattern string, potentially containing wildcards ('*'). + + Returns: + A tuple containing: + - A list of integers representing the text characters. + - A list of integers representing the pattern characters, with 0 for wildcards. + """ + + unique_chars = set(text + pattern) + char_to_int = {char: i + 1 for i, char in enumerate(unique_chars)} # Unique non-zero integers + + # Replace pattern '*' with 0, other characters with their unique integers + pattern_int = [char_to_int[char] if char != '*' else 0 for char in pattern] + text_int = [char_to_int[char] for char in text] + + return text_int, pattern_int + +def fft_convolution(a, b): + """Performs convolution using the Fast Fourier Transform (FFT). + + Args: + a: The first sequence. + b: The second sequence. + + Returns: + The convolution of the two sequences. + """ + + n = len(a) + len(b) - 1 + A = fft(a, n) + B = fft(b, n) + return np.real(ifft(A * B)) + +def compute_A_fft(text_int, pattern_int): + """Computes the A array for the pattern matching algorithm. + + Args: + text_int: The integer representation of the text. + pattern_int: The integer representation of the pattern. + + Returns: + The A array. + """ + + n = len(text_int) + m = len(pattern_int) + + # Power transforms of the pattern and text based on the formula + p1 = np.array(pattern_int) + p2 = np.array([p**2 for p in pattern_int]) + p3 = np.array([p**3 for p in pattern_int]) + + t1 = np.array(text_int) + t2 = np.array([t**2 for t in text_int]) + t3 = np.array([t**3 for t in text_int]) + + # Convolution to calculate the terms for A[i] + sum1 = fft_convolution(p3[::-1], t1) + sum2 = fft_convolution(p2[::-1], t2) + sum3 = fft_convolution(p1[::-1], t3) + + # Calculate A[i] using the convolution results + A = sum1[:n - m + 1] - 2 * sum2[:n - m + 1] + sum3[:n - m + 1] + + return A + +# Main function to run the matching +if __name__ == "__main__": + + import doctest + doctest.testmod() + # Get text and pattern as input from the user + # text = input("Enter the text: ") + # pattern = input("Enter the pattern (use '*' for wildcard): ") + + text = "abcabc" + pattern = "abc*" + + + + + text_int, pattern_int = preprocess_text_and_pattern(text, pattern) + A = compute_A_fft(text_int, pattern_int) + + # Matches occur where A[i] == 0 + matches = [i for i in range(len(A)) if np.isclose(A[i], 0)] + print("Pattern matches at indices:", matches) + \ No newline at end of file From 276f46a84b0fca5a6e6b3112189cd7f5234a816b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 12 Oct 2024 16:25:05 +0000 Subject: [PATCH 2/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/wildcard_pattern_matching_fft.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/strings/wildcard_pattern_matching_fft.py b/strings/wildcard_pattern_matching_fft.py index fb9e309a1e5f..24f62d5588cf 100644 --- a/strings/wildcard_pattern_matching_fft.py +++ b/strings/wildcard_pattern_matching_fft.py @@ -1,6 +1,7 @@ import numpy as np from numpy.fft import fft, ifft + def preprocess_text_and_pattern(text, pattern): """Preprocesses text and pattern for pattern matching. @@ -15,14 +16,17 @@ def preprocess_text_and_pattern(text, pattern): """ unique_chars = set(text + pattern) - char_to_int = {char: i + 1 for i, char in enumerate(unique_chars)} # Unique non-zero integers + char_to_int = { + char: i + 1 for i, char in enumerate(unique_chars) + } # Unique non-zero integers # Replace pattern '*' with 0, other characters with their unique integers - pattern_int = [char_to_int[char] if char != '*' else 0 for char in pattern] + pattern_int = [char_to_int[char] if char != "*" else 0 for char in pattern] text_int = [char_to_int[char] for char in text] return text_int, pattern_int + def fft_convolution(a, b): """Performs convolution using the Fast Fourier Transform (FFT). @@ -39,6 +43,7 @@ def fft_convolution(a, b): B = fft(b, n) return np.real(ifft(A * B)) + def compute_A_fft(text_int, pattern_int): """Computes the A array for the pattern matching algorithm. @@ -68,14 +73,15 @@ def compute_A_fft(text_int, pattern_int): sum3 = fft_convolution(p1[::-1], t3) # Calculate A[i] using the convolution results - A = sum1[:n - m + 1] - 2 * sum2[:n - m + 1] + sum3[:n - m + 1] + A = sum1[: n - m + 1] - 2 * sum2[: n - m + 1] + sum3[: n - m + 1] return A + # Main function to run the matching if __name__ == "__main__": - import doctest + doctest.testmod() # Get text and pattern as input from the user # text = input("Enter the text: ") @@ -84,13 +90,9 @@ def compute_A_fft(text_int, pattern_int): text = "abcabc" pattern = "abc*" - - - text_int, pattern_int = preprocess_text_and_pattern(text, pattern) A = compute_A_fft(text_int, pattern_int) # Matches occur where A[i] == 0 matches = [i for i in range(len(A)) if np.isclose(A[i], 0)] print("Pattern matches at indices:", matches) - \ No newline at end of file From 9254927f1b9f189db0a5a387ef135b03cbae8c50 Mon Sep 17 00:00:00 2001 From: Avaya Aggarwal <119044997+OnePunchMonk@users.noreply.github.com> Date: Sat, 12 Oct 2024 21:59:08 +0530 Subject: [PATCH 3/8] Update wildcard pattern matching algorithm with type hints and doctests --- strings/wildcard_pattern_matching_fft.py | 68 ++++++++++++++---------- 1 file changed, 40 insertions(+), 28 deletions(-) diff --git a/strings/wildcard_pattern_matching_fft.py b/strings/wildcard_pattern_matching_fft.py index fb9e309a1e5f..4b7b5b4ffeb9 100644 --- a/strings/wildcard_pattern_matching_fft.py +++ b/strings/wildcard_pattern_matching_fft.py @@ -1,7 +1,7 @@ import numpy as np from numpy.fft import fft, ifft -def preprocess_text_and_pattern(text, pattern): +def preprocess_text_and_pattern(text: str, pattern: str) -> tuple[list[int], list[int]]: """Preprocesses text and pattern for pattern matching. Args: @@ -12,8 +12,14 @@ def preprocess_text_and_pattern(text, pattern): A tuple containing: - A list of integers representing the text characters. - A list of integers representing the pattern characters, with 0 for wildcards. - """ + Examples: + >>> preprocess_text_and_pattern("abcabc", "abc*") + ([1, 2, 3, 1, 2, 3], [1, 2, 3, 0]) + >>> preprocess_text_and_pattern("hello", "he*o") + ([3, 2, 4, 4, 5], [3, 2, 0, 5]) + """ + unique_chars = set(text + pattern) char_to_int = {char: i + 1 for i, char in enumerate(unique_chars)} # Unique non-zero integers @@ -23,23 +29,29 @@ def preprocess_text_and_pattern(text, pattern): return text_int, pattern_int -def fft_convolution(a, b): + +def fft_convolution(input_seq_a: np.ndarray, input_seq_b: np.ndarray) -> np.ndarray: """Performs convolution using the Fast Fourier Transform (FFT). Args: - a: The first sequence. - b: The second sequence. + input_seq_a: The first sequence (1D numpy array). + input_seq_b: The second sequence (1D numpy array). Returns: The convolution of the two sequences. - """ - n = len(a) + len(b) - 1 - A = fft(a, n) - B = fft(b, n) + Examples: + >>> fft_convolution(np.array([1, 2, 3]), np.array([0, 1, 0.5])) + array([0. , 1. , 2.5, 3. , 1.5]) + """ + + n = len(input_seq_a) + len(input_seq_b) - 1 + A = fft(input_seq_a, n) + B = fft(input_seq_b, n) return np.real(ifft(A * B)) -def compute_A_fft(text_int, pattern_int): + +def compute_a_fft(text_int: list[int], pattern_int: list[int]) -> np.ndarray: """Computes the A array for the pattern matching algorithm. Args: @@ -48,19 +60,23 @@ def compute_A_fft(text_int, pattern_int): Returns: The A array. - """ + Examples: + >>> compute_a_fft([1, 2, 3, 1, 2, 3], [1, 2, 3, 0]) + array([...]) # Replace with the expected output based on your implementation + """ + n = len(text_int) m = len(pattern_int) # Power transforms of the pattern and text based on the formula p1 = np.array(pattern_int) - p2 = np.array([p**2 for p in pattern_int]) - p3 = np.array([p**3 for p in pattern_int]) + p2 = np.array([p ** 2 for p in pattern_int]) + p3 = np.array([p ** 3 for p in pattern_int]) t1 = np.array(text_int) - t2 = np.array([t**2 for t in text_int]) - t3 = np.array([t**3 for t in text_int]) + t2 = np.array([t ** 2 for t in text_int]) + t3 = np.array([t ** 3 for t in text_int]) # Convolution to calculate the terms for A[i] sum1 = fft_convolution(p3[::-1], t1) @@ -74,23 +90,19 @@ def compute_A_fft(text_int, pattern_int): # Main function to run the matching if __name__ == "__main__": - - import doctest - doctest.testmod() - # Get text and pattern as input from the user - # text = input("Enter the text: ") - # pattern = input("Enter the pattern (use '*' for wildcard): ") - + # Example test case text = "abcabc" pattern = "abc*" - - - + # Preprocess text and pattern text_int, pattern_int = preprocess_text_and_pattern(text, pattern) - A = compute_A_fft(text_int, pattern_int) + print("Preprocessed text:", text_int) + print("Preprocessed pattern:", pattern_int) + + # Compute A array + A = compute_a_fft(text_int, pattern_int) + print("A array:", A) - # Matches occur where A[i] == 0 + # Find matches matches = [i for i in range(len(A)) if np.isclose(A[i], 0)] print("Pattern matches at indices:", matches) - \ No newline at end of file From 56441f81921c47c3a64312321c055754ed9bee54 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 12 Oct 2024 16:31:39 +0000 Subject: [PATCH 4/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/wildcard_pattern_matching_fft.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/strings/wildcard_pattern_matching_fft.py b/strings/wildcard_pattern_matching_fft.py index 4b7b5b4ffeb9..c9ed59785026 100644 --- a/strings/wildcard_pattern_matching_fft.py +++ b/strings/wildcard_pattern_matching_fft.py @@ -1,6 +1,7 @@ import numpy as np from numpy.fft import fft, ifft + def preprocess_text_and_pattern(text: str, pattern: str) -> tuple[list[int], list[int]]: """Preprocesses text and pattern for pattern matching. @@ -19,12 +20,14 @@ def preprocess_text_and_pattern(text: str, pattern: str) -> tuple[list[int], lis >>> preprocess_text_and_pattern("hello", "he*o") ([3, 2, 4, 4, 5], [3, 2, 0, 5]) """ - + unique_chars = set(text + pattern) - char_to_int = {char: i + 1 for i, char in enumerate(unique_chars)} # Unique non-zero integers + char_to_int = { + char: i + 1 for i, char in enumerate(unique_chars) + } # Unique non-zero integers # Replace pattern '*' with 0, other characters with their unique integers - pattern_int = [char_to_int[char] if char != '*' else 0 for char in pattern] + pattern_int = [char_to_int[char] if char != "*" else 0 for char in pattern] text_int = [char_to_int[char] for char in text] return text_int, pattern_int @@ -44,7 +47,7 @@ def fft_convolution(input_seq_a: np.ndarray, input_seq_b: np.ndarray) -> np.ndar >>> fft_convolution(np.array([1, 2, 3]), np.array([0, 1, 0.5])) array([0. , 1. , 2.5, 3. , 1.5]) """ - + n = len(input_seq_a) + len(input_seq_b) - 1 A = fft(input_seq_a, n) B = fft(input_seq_b, n) @@ -65,18 +68,18 @@ def compute_a_fft(text_int: list[int], pattern_int: list[int]) -> np.ndarray: >>> compute_a_fft([1, 2, 3, 1, 2, 3], [1, 2, 3, 0]) array([...]) # Replace with the expected output based on your implementation """ - + n = len(text_int) m = len(pattern_int) # Power transforms of the pattern and text based on the formula p1 = np.array(pattern_int) - p2 = np.array([p ** 2 for p in pattern_int]) - p3 = np.array([p ** 3 for p in pattern_int]) + p2 = np.array([p**2 for p in pattern_int]) + p3 = np.array([p**3 for p in pattern_int]) t1 = np.array(text_int) - t2 = np.array([t ** 2 for t in text_int]) - t3 = np.array([t ** 3 for t in text_int]) + t2 = np.array([t**2 for t in text_int]) + t3 = np.array([t**3 for t in text_int]) # Convolution to calculate the terms for A[i] sum1 = fft_convolution(p3[::-1], t1) @@ -84,10 +87,11 @@ def compute_a_fft(text_int: list[int], pattern_int: list[int]) -> np.ndarray: sum3 = fft_convolution(p1[::-1], t3) # Calculate A[i] using the convolution results - A = sum1[:n - m + 1] - 2 * sum2[:n - m + 1] + sum3[:n - m + 1] + A = sum1[: n - m + 1] - 2 * sum2[: n - m + 1] + sum3[: n - m + 1] return A + # Main function to run the matching if __name__ == "__main__": # Example test case From ccb29d2656411f7720b52460aee08de9a2e4ea28 Mon Sep 17 00:00:00 2001 From: Avaya Aggarwal <119044997+OnePunchMonk@users.noreply.github.com> Date: Sat, 12 Oct 2024 22:04:27 +0530 Subject: [PATCH 5/8] Fix style issues and update variable names --- strings/wildcard_pattern_matching_fft.py | 53 ++++++++---------------- 1 file changed, 17 insertions(+), 36 deletions(-) diff --git a/strings/wildcard_pattern_matching_fft.py b/strings/wildcard_pattern_matching_fft.py index 4b7b5b4ffeb9..083c257cef2c 100644 --- a/strings/wildcard_pattern_matching_fft.py +++ b/strings/wildcard_pattern_matching_fft.py @@ -12,14 +12,7 @@ def preprocess_text_and_pattern(text: str, pattern: str) -> tuple[list[int], lis A tuple containing: - A list of integers representing the text characters. - A list of integers representing the pattern characters, with 0 for wildcards. - - Examples: - >>> preprocess_text_and_pattern("abcabc", "abc*") - ([1, 2, 3, 1, 2, 3], [1, 2, 3, 0]) - >>> preprocess_text_and_pattern("hello", "he*o") - ([3, 2, 4, 4, 5], [3, 2, 0, 5]) """ - unique_chars = set(text + pattern) char_to_int = {char: i + 1 for i, char in enumerate(unique_chars)} # Unique non-zero integers @@ -29,27 +22,20 @@ def preprocess_text_and_pattern(text: str, pattern: str) -> tuple[list[int], lis return text_int, pattern_int - -def fft_convolution(input_seq_a: np.ndarray, input_seq_b: np.ndarray) -> np.ndarray: +def fft_convolution(a: list[int], b: list[int]) -> np.ndarray: """Performs convolution using the Fast Fourier Transform (FFT). Args: - input_seq_a: The first sequence (1D numpy array). - input_seq_b: The second sequence (1D numpy array). + a: The first sequence. + b: The second sequence. Returns: The convolution of the two sequences. - - Examples: - >>> fft_convolution(np.array([1, 2, 3]), np.array([0, 1, 0.5])) - array([0. , 1. , 2.5, 3. , 1.5]) """ - - n = len(input_seq_a) + len(input_seq_b) - 1 - A = fft(input_seq_a, n) - B = fft(input_seq_b, n) - return np.real(ifft(A * B)) - + n = len(a) + len(b) - 1 + a_fft = fft(a, n) + b_fft = fft(b, n) + return np.real(ifft(a_fft * b_fft)) def compute_a_fft(text_int: list[int], pattern_int: list[int]) -> np.ndarray: """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: Returns: The A array. - - Examples: - >>> compute_a_fft([1, 2, 3, 1, 2, 3], [1, 2, 3, 0]) - array([...]) # Replace with the expected output based on your implementation """ - n = len(text_int) m = len(pattern_int) # Power transforms of the pattern and text based on the formula p1 = np.array(pattern_int) - p2 = np.array([p ** 2 for p in pattern_int]) - p3 = np.array([p ** 3 for p in pattern_int]) + p2 = np.array([p**2 for p in pattern_int]) + p3 = np.array([p**3 for p in pattern_int]) t1 = np.array(text_int) - t2 = np.array([t ** 2 for t in text_int]) - t3 = np.array([t ** 3 for t in text_int]) + t2 = np.array([t**2 for t in text_int]) + t3 = np.array([t**3 for t in text_int]) # Convolution to calculate the terms for A[i] sum1 = fft_convolution(p3[::-1], t1) sum2 = fft_convolution(p2[::-1], t2) sum3 = fft_convolution(p1[::-1], t3) - # Calculate A[i] using the convolution results - A = sum1[:n - m + 1] - 2 * sum2[:n - m + 1] + sum3[:n - m + 1] + # Calculate a[i] using the convolution results + a = sum1[:n - m + 1] - 2 * sum2[:n - m + 1] + sum3[:n - m + 1] - return A + return a # Main function to run the matching if __name__ == "__main__": @@ -100,9 +81,9 @@ def compute_a_fft(text_int: list[int], pattern_int: list[int]) -> np.ndarray: print("Preprocessed pattern:", pattern_int) # Compute A array - A = compute_a_fft(text_int, pattern_int) - print("A array:", A) + a = compute_a_fft(text_int, pattern_int) + print("A array:", a) # Find matches - matches = [i for i in range(len(A)) if np.isclose(A[i], 0)] + matches = [i for i in range(len(a)) if np.isclose(a[i], 0)] print("Pattern matches at indices:", matches) From 9d6751ce89bff596e74ed5d8c059ed1848405d19 Mon Sep 17 00:00:00 2001 From: Avaya Aggarwal <119044997+OnePunchMonk@users.noreply.github.com> Date: Sat, 12 Oct 2024 22:11:08 +0530 Subject: [PATCH 6/8] Fixed Issues --- strings/wildcard_pattern_matching_fft.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/strings/wildcard_pattern_matching_fft.py b/strings/wildcard_pattern_matching_fft.py index 780593d43fb3..8e9d86d05c33 100644 --- a/strings/wildcard_pattern_matching_fft.py +++ b/strings/wildcard_pattern_matching_fft.py @@ -26,25 +26,22 @@ def preprocess_text_and_pattern(text: str, pattern: str) -> tuple[list[int], lis return text_int, pattern_int -def fft_convolution(a: list[int], b: list[int]) -> np.ndarray: +def fft_convolution(first_seq: list[int], second_seq: list[int]) -> np.ndarray: """Performs convolution using the Fast Fourier Transform (FFT). Args: - a: The first sequence. + first_seq: The first sequence. b: The second sequence. Returns: The convolution of the two sequences. """ - n = len(a) + len(b) - 1 - a_fft = fft(a, n) - b_fft = fft(b, n) - return np.real(ifft(a_fft * b_fft)) + n = len(first_seq) + len(second_seq) - 1 + first_seq_fft = fft(first_seq, n) + second_seq_fft = fft(second_seq, n) + return np.real(ifft(first_seq_fft * second_seq_fft)) + - n = len(input_seq_a) + len(input_seq_b) - 1 - A = fft(input_seq_a, n) - B = fft(input_seq_b, n) - return np.real(ifft(A * B)) def compute_a_fft(text_int: list[int], pattern_int: list[int]) -> np.ndarray: @@ -86,6 +83,13 @@ def compute_a_fft(text_int: list[int], pattern_int: list[int]) -> np.ndarray: # Main function to run the matching if __name__ == "__main__": # Example test case + import doctest + doctest.testmod() + # Get text and pattern as input from the user + # text = input("Enter the text: ") + # pattern = input("Enter the pattern (use '*' for wildcard): ") + + text = "abcabc" pattern = "abc*" From f4efe932aaa9256fac30b7bec2b3fa952dfa25cc Mon Sep 17 00:00:00 2001 From: Avaya Aggarwal <119044997+OnePunchMonk@users.noreply.github.com> Date: Sat, 12 Oct 2024 22:16:04 +0530 Subject: [PATCH 7/8] Resolved merge conflict in wildcard_pattern_matching_fft.py --- strings/wildcard_pattern_matching_fft.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/strings/wildcard_pattern_matching_fft.py b/strings/wildcard_pattern_matching_fft.py index 8e9d86d05c33..1bc39270ebfa 100644 --- a/strings/wildcard_pattern_matching_fft.py +++ b/strings/wildcard_pattern_matching_fft.py @@ -12,7 +12,8 @@ def preprocess_text_and_pattern(text: str, pattern: str) -> tuple[list[int], lis Returns: A tuple containing: - A list of integers representing the text characters. - - A list of integers representing the pattern characters, with 0 for wildcards. + - A list of integers representing the pattern characters, + with 0 for wildcards. """ unique_chars = set(text + pattern) @@ -52,7 +53,7 @@ def compute_a_fft(text_int: list[int], pattern_int: list[int]) -> np.ndarray: pattern_int: The integer representation of the pattern. Returns: - The A array. + The a array. """ n = len(text_int) @@ -75,7 +76,7 @@ def compute_a_fft(text_int: list[int], pattern_int: list[int]) -> np.ndarray: # Calculate a[i] using the convolution results a = sum1[:n - m + 1] - 2 * sum2[:n - m + 1] + sum3[:n - m + 1] # Calculate A[i] using the convolution results - A = sum1[: n - m + 1] - 2 * sum2[: n - m + 1] + sum3[: n - m + 1] + a = sum1[: n - m + 1] - 2 * sum2[: n - m + 1] + sum3[: n - m + 1] return a From 756c0ffbf5641ec16ad596374ef71e3297dbfafb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 12 Oct 2024 16:46:52 +0000 Subject: [PATCH 8/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/wildcard_pattern_matching_fft.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/strings/wildcard_pattern_matching_fft.py b/strings/wildcard_pattern_matching_fft.py index 1bc39270ebfa..fb6ee780d181 100644 --- a/strings/wildcard_pattern_matching_fft.py +++ b/strings/wildcard_pattern_matching_fft.py @@ -27,6 +27,7 @@ def preprocess_text_and_pattern(text: str, pattern: str) -> tuple[list[int], lis return text_int, pattern_int + def fft_convolution(first_seq: list[int], second_seq: list[int]) -> np.ndarray: """Performs convolution using the Fast Fourier Transform (FFT). @@ -43,8 +44,6 @@ def fft_convolution(first_seq: list[int], second_seq: list[int]) -> np.ndarray: return np.real(ifft(first_seq_fft * second_seq_fft)) - - def compute_a_fft(text_int: list[int], pattern_int: list[int]) -> np.ndarray: """Computes the A array for the pattern matching algorithm. @@ -74,7 +73,7 @@ def compute_a_fft(text_int: list[int], pattern_int: list[int]) -> np.ndarray: sum3 = fft_convolution(p1[::-1], t3) # Calculate a[i] using the convolution results - a = sum1[:n - m + 1] - 2 * sum2[:n - m + 1] + sum3[:n - m + 1] + a = sum1[: n - m + 1] - 2 * sum2[: n - m + 1] + sum3[: n - m + 1] # Calculate A[i] using the convolution results a = sum1[: n - m + 1] - 2 * sum2[: n - m + 1] + sum3[: n - m + 1] @@ -85,12 +84,12 @@ def compute_a_fft(text_int: list[int], pattern_int: list[int]) -> np.ndarray: if __name__ == "__main__": # Example test case import doctest + doctest.testmod() # Get text and pattern as input from the user # text = input("Enter the text: ") # pattern = input("Enter the pattern (use '*' for wildcard): ") - text = "abcabc" pattern = "abc*"