Answer to Question #166514 in Python for M

Question #166514

a = 'ramisgood'

b = 'goodforall'


c = 'finally'

d = 'restforall'


e = "correct"

f = "wrong"


def longestSubstringFinder(string1, string2):

answer = ""

len1, len2 = len(string1), len(string2)

for i in range(len1):

match = ""

for j in range(len2):

if (i + j < len1 and string1[i + j] == string2[j]):

match += string2[j]

else:

if (len(match) > len(answer)): answer = match

match = ""

if answer == '':

return 'No overlapping'

else:

return answer


print(longestSubstringFinder(a, b))

print(longestSubstringFinder(c, d))

print(longestSubstringFinder(e,f))



when printing print(longestSubstringFinder(e,f)) getting "r" as output

but , output should be as "No overlapping"


1
Expert's answer
2021-02-25T02:41:56-0500
def overlap(string1, string2):
    """Longest string which is suffix for string1 and prefix for string2
    Your testcases
    >>> overlap('ramisgood', 'goodforall')
    'good'
    >>> overlap('finally', 'restforall')
    'No overlapping'
    >>> overlap('correct', 'wrong')
    'No overlapping'

    Edge cases
    >>> overlap('final', 'final')
    'final'
    >>> overlap('toast', 'tst')
    't'
    >>> overlap('', '')
    'No overlapping'
    >>> overlap('first', '')
    'No overlapping'
    >>> overlap('', 'second')
    'No overlapping'
    """
    minLen = min(len(string1), len(string2))
    for i in range(minLen, 0, -1):
        suffix = string1[-i:]
        if suffix == string2[:i]:
            return suffix
    return 'No overlapping'

if __name__ == '__main__':
    import doctest
    doctest.testmod(verbose=True)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS