Hi i have to check the overlapping and duplicate of string from the data , i could do it can anyone help me to find the duplicate of string .I have this data s = [(100, 350,"a"), (125, 145,"a"), (200, 400, "d"), (0, 10, "a")]
and i done the overlap part but theduplicate check odf string i need help .
def overlap(a, b) -> bool:
a_start, a_end, _ = a
b_start, b_end, _ = b
return a_start < b_end and b_start < a_end
ls = [(100, 350,"a"), (125, 145,"a"), (200, 400, "d"), (0, 10, "a")]
overlaps = set()
for idx_a in range(len(ls)):
for idx_b in range(len(ls)):
if idx_a != idx_b:
if overlap(ls[idx_a], ls[idx_b]):
overlaps.add(ls[idx_a])
overlaps.add(ls[idx_b])
print(f"Number of overlaps: {len(overlaps)}")
CodePudding user response:
It seems like you don't need to use set in case you need only number of overlaps.
I would solve your problem like this:
def is_overlapped(a, b) -> bool: # changed the name for readability
a_start, a_end, _ = a
b_start, b_end, _ = b
return a_start < b_end and b_start < a_end
ls = [(100, 350,"a"), (125, 145,"a"), (200, 400, "d"), (0, 10, "a")]
overlaps = 0 # int instead of set
for idx_a, value_a in enumerate(ls): # this is more pythonic way to access the index and item at the same time
for idx_b, value_b in enumerate(ls):
if idx_a != idx_b:
if is_overlapped(value_a, value_b):
overlaps = 1
print(f"Number of overlaps: {overlaps}")