Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Advanced level school Python programming. need helps. tq 3 In this task, you are not allowed to use the keyword in to determine whether a

image text in transcribed

image text in transcribed

Advanced level school Python programming. need helps. tq

3 In this task, you are not allowed to use the keyword in to determine whether a character exists in a string, or whether one string is a substring of another (e.g. 'G' in 'SINGAPORE' or 'GAP' in 'SINGAPORE'). You are also not allowed to check directly if two substrings longer than one character are equal (e.g. 'SINGAPORE' [0:3] == 'HELSINKI' [3:6] ). The Boyer-Moore algorithm was designed to check if a string s is a substring of another string t (called the text). Task 3.1 Write a function dic ( s ) that creates a dictionary whose keys are the characters of s, and the value of each key is the difference between the index of its last occurrence in s, from the largest index in s. For example, dic ('anman') creates a dictionary with the following keys and values: Task 3.2 To determine whether s is a substring of t, we first compare the last letter of s with the character in t that has the corresponding index. The following shows two examples. If they match, we move on to the second last letter, and so on, until there is a mismatch. If we reach the first character of s and there is no mismatch, then s is a substring of t. If there is a mismatch (as illustrated in both examples above), then we shift s forward. For t1, the mismatched character (' p ') does not appear in s at all. Therefore, we can shift s forward until the first character of s is after the mismatched character, and start checking again from the last character of s. In this case, we find that we can reach the first character of s without a mismatch, and therefore s is a substring of t. For t2, the mismatched character (' m ') occurs in s. We therefore need to shift s so that the last occurrence of ' m ' in ' s ' lines up with the ' m ' in t2, and start checking again from the last character of s. In this case, there is a mismatch immediately, and the mismatched character ('m') occurs in s. We therefore need to shift s so that the last occurrence of ' m ' in ' s ' lines up with this ' m ' in t2. We start checking again from the last character of s. There is an edge case in which the mismatch happens in which the mismatched character in t(a) is a character which occurred to its right in s. * (mismatched character is the ' a ') t3: a n p a m a n m a n s:anm a n (result of applying the shifting rule) t3: a n p a m a n m a n s:anman In this case, the shifting rule would shift s backwards, which is counterproductive. In this case, the best we can do is shift s one character forward and start checking again from the last character. *t3:anpamanmans:anman The loop of checking and shifting continues until we shift s so that its last character is beyond the last character of t. Write code to carry out this algorithm to check if s is a substring of t. Test your code on the following test cases: s=anmant1=amant2=anpanmant3=anpanpant4=anpamanman

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

More Books

Students also viewed these Databases questions

Question

3. Define the attributions we use to explain behavior

Answered: 1 week ago