HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Adding intervals to an interval store

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
intervalsintervaladdingstore

Problem

Write a program to do addition of intervals to an interval store.


An interval is represented by an array of two elements - the lower and
the upper bound (you can assume integers). Assume that intervals are
constructed properly (e.g. the lower bound is not greater than the
upper bound, etc.).


Implement the #add method that will add a new interval, handling the
merges properly.


Example:

> store = IntervalStore.new
[]
> store.add(1, 2)
[[1, 2]]
> store.add(5, 6)
[[1, 2], [5, 6]]
> store.add(1, 4)
[[1, 4], [5, 6]]
> store.add(1, 2)
[[1, 4], [5, 6]]
> store.add(3, 5)
[[1, 6]]
> store.add(0, 7)
[[0, 7]]


Here is my Python code. It works but is not perfect.

```
class IntervalStore(object):
def __init__(self):
self.store = []

def filter_result(self, flatten_store, low_index, upp_index):
useless_values = filter(lambda value: low_index = 0 and upp_index >= 0:
if low_index % 2 != 0:
low_index -= 1
if upp_index % 2 == 0:
upp_index += 1
elif low_index >= 0 > upp_index:
if low_index % 2 != 0:
low_index -= 1
flatten_store.append(upp)
flatten_store = sorted(flatten_store)
upp_index = self.index_of(upp, flatten_store)
if upp_index % 2 != 0:
upp_index += 1
elif low_index < 0 <= upp_index:
if upp_index % 2 == 0:
upp_index += 1
flatten_store.append(low)
flatten_store = sorted(flatten_store)
low_index = self.index_of(low, flatten_store)
if low_index % 2 != 0:
low_index -= 1
upp_index += 1
else:
flatten_store.append(low)
flatten_store.append(upp)
flatten_store = sorted(flatten_store)
low_index, upp_index = self.index_of(low, flatten_store), self.index_of(upp, flatten_store)

if low_index % 2 != 0:

Solution

If you have range [a,b], and want to add a range [c,d], there are 6 possible scenarios if you make the assumption, a

  • 2 - if subsection found return



  • 3 - if left/right/total overlap found remove the current range being evaluated from the range list, merge the two ranges, then call add with the larger range.



  • 4 - if ends up being an over/under shoot for all ranges, add the range



class IntervalStore(object):
    def __init__(self):
        self.range_list = []
    def __str__(self):
        return str(self.range_list)

    def __repr__(self):
        return str(self)

    def add(self, low, high):
        for interval in self.range_list:
            if interval[0] = high:
                return
            if interval[0] > high or interval[1]  self.range_list[insert_index][0]:
                insert_index += 1
            self.range_list.insert(insert_index, [low, high])


EDIT If you wanted to avoid an error regarding
low > high input, then add
low, high = sorted([low,high]) at the beginning of the add function.

Also add
if low == high: return`

Code Snippets

b < c - undershoot
a < c, b >= c, b <= d - left overlap
a <= c, b >= d - total overlap
a >= c, b <= d - subsection
a > c, b > d - right overlap
a > d - overshoot
class IntervalStore(object):
    def __init__(self):
        self.range_list = []
    def __str__(self):
        return str(self.range_list)

    def __repr__(self):
        return str(self)

    def add(self, low, high):
        for interval in self.range_list:
            if interval[0] <= low and interval[1] >= high:
                return
            if interval[0] > high or interval[1] < low:
                continue
            else:
                self.range_list.remove(interval)
                self.add(min([interval[0], low]), max([interval[1], high]))
                return
        if len(self.range_list) == 0:
            self.range_list.append( [low, high])
        else:
            insert_index = 0
            while insert_index < len(self.range_list) and high > self.range_list[insert_index][0]:
                insert_index += 1
            self.range_list.insert(insert_index, [low, high])

Context

StackExchange Code Review Q#46463, answer score: 6

Revisions (0)

No revisions yet.