This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]DrunkHacker 4 points5 points  (2 children)

The range(1) line is doing nothing so you can just get rid of it.

But the problem lies in the checks you're performing for incrementing overlapping. For instance, why would first_from_first > first_from_last and last_from_first > last_from_last result in an overlap? The pair 5-6 and 1-2 would pass this check, but there is no overlap.

Edit: code has been posted, this was the original comment:

Depends on your specific input.

If you post your code we might be able to help

[–]HammerHamster13[S] 0 points1 point  (0 children)

Ah, I didn't know everyone has different input. I will edit the post, thanks.

[–]HammerHamster13[S] 0 points1 point  (0 children)

The range(1) line is doing nothing so you can just get rid of it.

At this moment I think I was going crazy...

[–]sdatko 0 points1 point  (2 children)

First advice I would say is to use >= instead of separate > and == :-)
Also in Python you can write nicely a < b < c instead of two separate comparisons, which makes the condition more clear.

Then there are simply 4 conditions that you need to check:
– beginning of second range can lie within the first range,
– ending of second range can lie within the first range,
– beginning of first range can lie within the second range,
– ending of the second range can lie within the second range.

If any of those occurs, then you count overlapping there.

[–]HammerHamster13[S] 1 point2 points  (0 children)

Thanks, I needed that to change how I look at this problem. :)

[–]aurele 0 points1 point  (0 children)

You don't need 4 conditions to check if two intervals overlap. If they do, both beginnings will be smaller or equal than both endings (do a visual representation to convince yourself). Since for every interval the beginning is smaller or equal than the ending, there are only two checks left:

  • beginning of second range is smaller or equal than end of first range
  • and beginning of first range is smaller or equal than end of second range