check_overlap - Check to see if two strings overlap, at a certain offset Please
ID: 3857635 • Letter: C
Question
check_overlap - Check to see if two strings overlap, at a certain offset
Please help me understand with steps and explainations if at all possible..the yellow section is the prompt
This is a duplicate of the docstring: Given two strings (or any slicable sequence type) and a nonzero "offset", compare the portions of the strings that overlap, when the 2nd string is offset by this amount. Returns True if the overlapped portions are identical; returns False if there are any differences. If the offset is positive, then the 2nd string is offset to the right - that is, characters are discarded from the front of the 1st string, and from the back of the 2nd. If the offset is negative, then the 2nd string is offset to the left- that is, characters are discarded from the back of the 1st string, and from the front of the 2nd. If the offset is zero, then the strings are simply compare The strings must have the same length (but must not be the empty string) the absolute value of the offset must be less than this length. (CLOUD CODER NOTE: In a real Python implementation, you would enforce this with exceptions. But in Cloud Coder, you can simply assume that this is true for all testcases.) s. Parameters: two strings (or slicable types), integer offset Return value: True or FalseExplanation / Answer
The answer is as follows:
class EmptyStringError(Exception):
"""The strings are empty""""
pass
class LengthNotEqualError(Exception):
"""The strings are of different lengths""""
pass
class OffsetMoreError(Exception):
"""The offset is more than or equal to the length of strings""""
pass
def check_overlap(s1,s2,offset):
try:
if len(s1) > 0 and len(s2) > 0:
if len(s1) == len(s2):
if abs(offset) < len(s1):
if offset == 0:
if s1 == s2:
return True
else:
return False
if offset > 0:
if s1[offset:] == s2[:offset]:
return True
else:
return False
if offset < 0:
if s1[:offset] == s2[offset:]:
return True
else:
return False
else:
raise OffsetMoreError
else:
raise LengthNotEqualError
else:
raise EmptyStringError
except EmptyStringError:
print("Empty string error ")
return False
except LengthNotEqualError:
print("Length of Strings not equal error ")
return False
except OffsetMoreError:
print("Offset more than or equal to length of strings ")
return False
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.