1

Suppose I have the following list of lists:

intervals = [[3, 5], [11,25], [39,40], [45, 48]]

Beginning with the start number, 0 for this example, all the way to the end number, 50 for this example, my goal is to create a list of lists with ranges of every number not in the current list of list. For example, the list I want to produce would like like this:

nonIntervals = [[0, 3], [5, 11], [25, 39], [40, 45], [48, 50]]

I have already been working on an implementation to do just this but it has issues with certain test cases:

for index in range(len(intervals)):
    nonIntervals.append([start, intervals[index][0]])
    start = intervals[index][1]
    
if not(nonIntervals[index][1] == end):
    nonIntervals.append([intervals[index][1], end])

With this example I run into some problems where it produces intervals I do not need. For example, if I used the following interval:

intervals = [[0, 5], [11,25], [39,40], [45, 48]]

I get this interval when running the loop: nonIntervals = [[0, 0], [5, 11], [25, 39], [40, 45], [48, 50]]

Does anyone know how I can avoid the nonIntervals list containing the [0,0] list? For some reason, it include the start of the array when I want to ignore it and begin with [5,11]. The same thing applies for when the end is at the end of the list.

For example: intervals = [[0, 5], [11,25], [39,40], [45, 50]]

When running my program it returns, nonIntervals = [[0, 0], [5, 11], [25, 39], [40, 45], [50, 50]]

I am expecting the last list in this list of lists to be [40, 45]. If anyone knows how to correct my loop or proper if-statements I need to implement I'd greatly appreciate it.

2
  • 2
    You almost got it. Just add an if condition before every non-intervals.append() statement, and do not append if the items that you're gonna append are the same Commented Jun 10, 2021 at 3:08
  • Okay, I think I corrected ! So for the last if statement that I have in my current code, do you think there is a way to implement that into the first for loop that I have or do you think it is a necessary statement to have? Commented Jun 10, 2021 at 3:34

1 Answer 1

1

Someone has already commented on your question by providing an answer to make the code work. But you might be interested in portion, a library I wrote to deal with intervals. It's on PyPI, so you can install it as usual: pip install portion.

Applied on your example:

>>> items = [[3, 5], [11,25], [39,40], [45, 48]]
>>> import portion as P
>>> interval = P.Interval(*[P.closed(x, y) for x, y in items])
>>> interval
[3,5] | [11,25] | [39,40] | [45,48]

Now, in order to get all ranges that are not in your list, it suffices to take the complement (either using ~ or .complement), and to restrict it to the range you're interested in (either using & or .intersection with the desired range):

>>> lower, upper = 0, 50
>>> ~interval & P.closed(lower, upper)
[0,3) | (5,11) | (25,39) | (40,45) | (48,50]

Since portion automatically simplifies intervals (including empty ones), you won't have any issue with [0,0].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.