Answer to Question #166821 in Python for prathyusha

Question #166821

Prefix Suffix

Write a program to check the overlapping of one string's suffix with the prefix of another string.Input


The first line of the input will contain a string A.

The second line of the input will contain a string B.Output


The output should contain overlapping word if present else print "No overlapping".Explanation


For example, if the given two strings, A and B, are "ramisgood" "goodforall"

The output should be "good" as good overlaps as a suffix of the first string and prefix of next.

Sample Input 1

ramisgood

goodforall

Sample Output 1

good

Sample Input 2

finally

restforall

Sample Output 2

No overlapping




1
Expert's answer
2021-02-26T19:54:30-0500
def overlap2(A, B):
    """Overlapping of the A string's suffix with the prefix of B string

    >>> overlap2('ramisgood', 'goodforall')
    'good'
    >>> overlap2('finally', 'restforall')
    'No overlapping'
    >>> overlap2('orion', 'orion')
    'orion'
    >>> overlap2('tora', 'ara')
    'a'
    >>> overlap2('', '')
    'No overlapping'
    >>> overlap2('in', '')
    'No overlapping'
    >>> overlap2('', 'out')
    'No overlapping'
    """
    lenA = len(A)
    for i in range(lenA - min(lenA, len(B)), lenA):
        equal = True
        for j in range(lenA - i):
            if A[i+j] != B[j]:
                equal = False
                break
        if equal:
            return A[i:]

    return 'No overlapping'

A = input('String A? ')
B = input('String B? ')
print(overlap2(A, B))

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

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