From c390b29d2948ad64e10afa6dc9f6ef5646fce69a Mon Sep 17 00:00:00 2001 From: asmit27rai Date: Mon, 17 Mar 2025 19:05:46 +0530 Subject: [PATCH 1/2] feat: Implement Raita Algorithm for string matching - Added `_raita` function to `pydatastructs/strings/algorithms.py`. - Updated `find` function to support the `raita` algorithm. - Added test cases for the Raita algorithm in `tests/test_strings.py`. - Ensured compatibility with existing string matching algorithms. --- pydatastructs/strings/algorithms.py | 45 +++++++++++++++++++ .../strings/tests/test_algorithms.py | 3 ++ 2 files changed, 48 insertions(+) diff --git a/pydatastructs/strings/algorithms.py b/pydatastructs/strings/algorithms.py index 1e26b9411..20a958530 100644 --- a/pydatastructs/strings/algorithms.py +++ b/pydatastructs/strings/algorithms.py @@ -245,3 +245,48 @@ def _z_function(text, query): positions.append(pos) return positions + +def _raita(text, query): + """ + Implements the Raita algorithm for string matching. + + Parameters + ========== + text: str + The text in which the pattern is to be searched. + query: str + The pattern to be searched in the text. + + Returns + ======= + DynamicOneDimensionalArray + An array of starting positions of the pattern in the text. + """ + positions = DynamicOneDimensionalArray(int, 0) + n, m = len(text), len(query) + + if m == 0 or n == 0 or m > n: + return positions + + bad_char = {} + for i in range(m): + bad_char[query[i]] = i + + middle_char = query[m // 2] + + i = 0 + while i <= n - m: + if query[0] == text[i] and query[-1] == text[i + m - 1] and middle_char == text[i + m // 2]: + j = 1 + while j < m - 1 and query[j] == text[i + j]: + j += 1 + if j == m - 1: + positions.append(i) + + if i + m < n: + shift = bad_char.get(text[i + m], -1) + i += max(1, m - 1 - shift) + else: + break + + return positions diff --git a/pydatastructs/strings/tests/test_algorithms.py b/pydatastructs/strings/tests/test_algorithms.py index 37622cf80..517f69bc2 100644 --- a/pydatastructs/strings/tests/test_algorithms.py +++ b/pydatastructs/strings/tests/test_algorithms.py @@ -14,6 +14,9 @@ def test_bm(): def test_zf(): _test_common_string_matching('z_function') +def test_raita(): + _test_common_string_matching('raita') + def _test_common_string_matching(algorithm): true_text_pattern_dictionary = { "Knuth-Morris-Pratt": "-Morris-", From 1900667508778d05fdfc8df237b8899490a9d296 Mon Sep 17 00:00:00 2001 From: asmit27rai Date: Wed, 19 Mar 2025 13:41:25 +0530 Subject: [PATCH 2/2] fix --- pydatastructs/strings/algorithms.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pydatastructs/strings/algorithms.py b/pydatastructs/strings/algorithms.py index 20a958530..9e08e8e3c 100644 --- a/pydatastructs/strings/algorithms.py +++ b/pydatastructs/strings/algorithms.py @@ -252,6 +252,7 @@ def _raita(text, query): Parameters ========== + text: str The text in which the pattern is to be searched. query: str @@ -259,6 +260,7 @@ def _raita(text, query): Returns ======= + DynamicOneDimensionalArray An array of starting positions of the pattern in the text. """