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"
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)
Comments
Leave a comment