-3

Given the min and max values of two ranges representing continuous intervals, I want to know if the second range is within the first. Note also that this question has nothing whatsoever to do with Python's range function.

Note that I don't have the start and end of each range. I am already receiving the min and max from upstream. There exist some similar questions, but they do not use min and max, and they don't distinguish between non-strict vs strict. I would like the simplest possible logic. Although any prior answers that use the start and end would also work, they would use more complicated logic, and are therefore not desirable.

To give a natural example, when a person is standing, the range of one's waist to knee is contained in the larger range of head to toe. The range of head to waist is however not contained in the range of neck to knee.

More formally, this can be checked non-strictly or strictly as per the tests below:

Non-strict:

def is_subrange(min1, max1, min2, max2):
    ...  # To be implemented.

assert is_subrange(2, 9, 5, 7) == True
assert is_subrange(2, 9, 1, 3) == False
assert is_subrange(2, 9, 7, 11) == False
assert is_subrange(2, 9, 1, 11) == False

assert is_subrange(2, 9, 6, 9) == True  # is not strict
assert is_subrange(2, 9, 2, 4) == True  # is not strict
assert is_subrange(2, 9, 2, 9) == True  # is not strict

Strict:

def is_strict_subrange(min1, max1, min2, max2):
    ...  # To be implemented.

assert is_strict_subrange(2, 9, 5, 7) == True  # is as is_subrange
assert is_strict_subrange(2, 9, 1, 3) == False  # is as is_subrange
assert is_strict_subrange(2, 9, 7, 11) == False  # is as is_subrange
assert is_strict_subrange(2, 9, 1, 11) == False  # is as is_subrange

assert is_strict_subrange(2, 9, 6, 9) == False  # is not as is_subrange
assert is_strict_subrange(2, 9, 2, 4) == False  # is not as is_subrange
assert is_strict_subrange(2, 9, 2, 9) == False  # is not as is_subrange

These don't work:

def is_subrange(min1, max1, min2, max2):
    return min1 <= min2 and max1 <= max2

def is_strict_subrange(min1, max1, min2, max2):
    return min1 < min2 and max1 < max2
Asclepius
  • 57,944
  • 17
  • 167
  • 143
  • What's the difference between start/end and min/max? – Barmar Oct 06 '22 at 01:50
  • I don't understand that. `min` and `start` are two different words for the same thing, aren't they? – Barmar Oct 06 '22 at 01:53
  • I think we're just having a language problem. Can you give an example where they're different? – Barmar Oct 06 '22 at 02:04
  • The only case where there would be a difference between start/end and min/max is if you have a range with steps other than 1. But you don't include the steps in your question, so I assume you mean step=1. – Barmar Oct 06 '22 at 02:06
  • Also, the python `range()` object doesn't include the end value. But that's just a subtraction by 1, it doesn't affect the subrange algorithm. And you don't seem to be asking about range objects, you're just giving the endpoints. – Barmar Oct 06 '22 at 02:07
  • Then I'm back to asking you: what's the difference between the start and end versus min and max? – Barmar Oct 06 '22 at 02:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/248584/discussion-between-asclepius-and-barmar). – Asclepius Oct 06 '22 at 02:10

1 Answers1

1

This might help.

def is_subrange(min1, max1, min2, max2):
    return min1 <= min2 and max1 >= max2


def is_strict_subrange(min1, max1, min2, max2):
    return min1 < min2 and max1 > max2
Murali S
  • 61
  • 3