Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement the Rabin - Karp search algorithm as presented in the exercise. A hash value is calculated for the pattern ( of length m )

Implement the Rabin-Karp search algorithm as presented in the exercise. A hash value is calculated for the pattern (of length
m), and for a partial sequence from the text (with the same length m). If the two hash values are equal, the brute-force method
is used to verify character by character if the pattern and the sequence are identical. Implement the rolling-hash function for
computing the hash values for base b=29, using the following skeleton: class RabinKarp:
# param pattern - The string pattern that is searched in the text.
# param text - The text string in which the pattern is searched.
# return a list with the starting indices of pattern occurrences in the text, or None if not found.
# raises ValueError if pattern or text is None or empty.
def search(self, pattern, text):
# param sequence - The char sequence for which the (rolling) hash shall be computed.
# param last_character - The character to be removed from the hash when a new character is added.
# param previous_hash - The most recent hash value to be reused in the new hash value.
# return hash value for the given character sequence using base 29.
def get_rolling_hash_value(self, sequence, last_character, previous_hash): The search method should return a list with the starting indices of the positions where the pattern was found in the input text,
or None if not found.
The alphabet of the input text and pattern is letters (upper- and lower case), spaces (), periods (.) and commas (,), and
the match must be case sensitive. None or empty strings in the pattern or text should trigger a ValueError exception. In case
of partially overlapping matches, as in the example below (see index 11 and 12), all of them should be counted.
Example: For sequence AbCdExxx, Xxxxxke and pattern xxx the search should return [5,11,12]. and here is the provided code: class RabinKarp:
def __init__(self):
pass
"""
This method uses the RabinKarp algorithm to search a given pattern in a given input text.
@ param pattern - The string pattern that is searched in the text.
@ param text - The text string in which the pattern is searched.
@ return a list with the starting indices of pattern occurrences in the text, or None if not found.
@ raises ValueError if pattern or text is None or empty.
"""
def search(self, pattern, text):
# TODO
pass
"""
This method calculates the (rolling) hash code for a given character sequence. For the calculation use the
base b=29.
@ param sequence - The char sequence for which the (rolling) hash shall be computed.
@ param last_character - The character to be removed from the hash when a new character is added.
@ param previous_hash - The most recent hash value to be reused in the new hash value.
@ return hash value for the given character sequence using base 29.
"""
@staticmethod
def get_rolling_hash_value(sequence, last_character, previous_hash):
# TODO
pass

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions

Question

Contrast intelligence and emotional intelligence.

Answered: 1 week ago

Question

Briefly describe four guides to ethical decision-making

Answered: 1 week ago