0

A question similar to What's the most efficient way to test two integer ranges for overlap? but a little more advanced:

Let we are given two inclusive integer number ranges [x1:x2] and [y1:y2], where x1 ≤ x2 and y1 ≤ y2.

x1 and/or y1 can be an integer or minus infinity (represented as None Python object), x2 and/or y2 can be an integer or plus infinity (represented as None Python object).

What is the best way to test whether there is any overlap of the two ranges?

I am sure I can write an algorithm, but maybe you will find a shorter Python code for me?

(In fact, I deal not with integers but with version strings like "3.7". But I think I will have no trouble moving from integers to version strings by myself.)

porton
  • 5,214
  • 11
  • 47
  • 95
  • One way is to create an object with overridden comparison operators wrapping an integer (or a version number) with possible `None` and then using the same formula as in https://stackoverflow.com/q/3269434/856090 – porton Jul 25 '18 at 19:26
  • If the step-size is always `1`, isn't this a mere set of inequations, chaining some conditions like `(x1 <= y1 and x2 >= y1) or (...)`? – user2390182 Jul 25 '18 at 19:30
  • If you're comparing version strings, consider using [existing tools for that](https://stackoverflow.com/questions/11887762/how-do-i-compare-version-numbers-in-python). – user2357112 Jul 25 '18 at 19:54
  • @user2357112 I already use `deb_pkg_tools.version.Version` to compare Debian (currently I support only Debian and derivatives) version strings – porton Jul 25 '18 at 19:56
  • There's already a solution: https://stackoverflow.com/questions/3269434/whats-the-most-efficient-way-to-test-two-integer-ranges-for-overlap – Andrej Kesely Jul 25 '18 at 20:00
  • @AndrejKesely I have already referred to this question in my question. The distinction of my question is that in my variant of the questions values can be infinite – porton Jul 25 '18 at 20:05
  • 1
    @porton: Yes, but an easy variant of that solution gives a solution for this problem: just use `(x1 is None or y2 is None or x1 <= y2) and (y1 is None or x2 is None or y1 <= x2)` – Mark Dickinson Jul 26 '18 at 12:17

1 Answers1

1

I think this should work, just checking the different relations that can exist between x1, x2, y1, y2. This can probably be written in shorter form but this code emphasize the different cases.

def checkIntersection(x1, x2, y1, y2):
    if x1 is not None:
        if y1 is not None:
            if x1 <= y1 and (x2 is None or x2 > y1):
                return True
            elif y1 <= x1 and (y2 is none or y2 > x1):
                return True
            else:
                return False
    else:
        if y1 is None:
            return True
        else:
            if x2 is None or x2 > y1:
                return True
            return False

And a more elegant solution -

import numpy as np

def checkIntersection(x1, x2, y1, y2):
    x1 = x1 if x1 is not None else -np.inf
    y1 = y1 if y1 is not None else -np.inf
    x2 = x2 if x2 is not None else np.inf
    y2 = y2 if y2 is not None else np.inf
    return (x1 <= y1 and x2 >= y1) or (y1 <= x1 and y2 >= x1)
Tom Ron
  • 5,906
  • 3
  • 22
  • 38