Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Q 2 ( b ) ( 1 4 marks ) Write and test a function longest _ common _ substring _ DP with string arguments
Qb marks
Write and test a function longestcommonsubstringDP with string arguments left and right, using a topdown dynamic programming algorithm, to find the longest common substring of the two strings. It should return the empty string if there is no common substring.
Chapter shows a variety of dynamic programming approaches to various problems, including the similar but different! problem of the longest common subsequence of two strings.
We would like you to write your function so as to use topdown dynamic programming using either a dictionary or a matrix approach for the cache, closely following one of the techniques illustrated in Chapter for the longest common subsequence problem. You will lose marks if you do not follow an approach similar to Chapter
Complete the function in the editable code cell below. You can use the same tests that were provided for Question c your tutor may use additional tests.
Here is an outline of a possible recursive solution to the problem, similar to the recursive solution to the longest common subsequence problem in Chapter This is not exactly the algorithm you need as it does not use cacheing, but it may be a useful start.
#def lcsubstringleft: str right: str str:
#Return the longest common substring of left and right."""
# if one or both strings are empty:
# return the empty string
# else
# find the LC leading substring of left and right
# compute the LC substring when skipping the right head
# compute the LC substring when skipping the left head
# return the longest of these three substrings
run i mutil
def longestcommonsubstringDPleft: str right: str str:
pass # Delete this and replace with your answer here
# Use the same test cases as in Qc
testlongestcommonsubstringDP longestcommonsubstringtests
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started