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 < flatten_store.index(value) < upp_index, flatten_store)
flatten_store = filter(lambda value: value not in useless_values, flatten_store)
self.store = []
for i, k in zip(flatten_store[0::2], flatten_store[1::2]):
self.store.append([i, k])
def add(self, low, upp):
flatten_store = [number for pair in self.store for number in pair]
low_index, upp_index = self.index_of(low, flatten_store), self.index_of(upp, flatten_store)
if 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:
low_index -= 1
if upp_index % 2 == 0:
upp_index += 1
self.filter_result(flatten_store, low_index, upp_index)
def index_of(self, value, list):
try:
return list.index(value)
except ValueError:
return -1
same as Ruby code:
class IntervalStore
def initialize
@store = []
end
def to_s
"[#{@store.map{ |i| "[#{i.first}, #{i.last}]" }.join(', ')}]"
end
# TODO - implement this method
def add(low, upp)
flatten_store = @store.flatten
low_index, upp_index = flatten_store.index(low), flatten_store.index(upp)
if low_index and upp_index
low_index -=1 if low_index.odd?
upp_index +=1 if upp_index.even?
elsif low_index and !upp_index
low_index -= 1 if low_index.odd?
(flatten_store << upp).sort!
upp_index = flatten_store.index(upp)
upp_index += 1 if upp_index.odd?
elsif !low_index and upp_index
upp_index += 1 if upp_index.even?
(flatten_store << low).sort!
low_index = flatten_store.index(low)
low_index -= 1 if low_index.odd?
upp_index += 1
else
(flatten_store << low << upp).sort!
low_index, upp_index = flatten_store.index(low), flatten_store.index(upp)
low_index -= 1 if low_index.odd?
upp_index += 1 if upp_index.even?
end
filter_result = filter_store(flatten_store, low_index, upp_index)
format_result(filter_result)
end
def format_result(rest)
@store = []
rest.each_slice(2) { |pair_values| @store << pair_values }
@store
end
def filter_store(flatten_store, low_index, upp_index)
flatten_store.clone.delete_if do |value|
flatten_store_index = flatten_store.index(value)
low_index < flatten_store_index and flatten_store_index < upp_index
end
end
end
I want to know how you would do it in other ways to make it smarter.