Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Interval tree server the purpose well, but here is another approach that I have used to add to the range.</p> <p>The assumption is that the list to which the new tuple(a rane) is being added is either empty or has no overlapping ranges. Secondly, input is of the form a, b where a &lt;= b You can convert the list of tuples to a single list of numbers and then add the new tuple to it.</p> <p>Let rangeList be the current list of ranges. eg. [1, 4, 6, 10, 12, 14] would mean a range list of [(1, 4), (6, 10), (12, 14)] </p> <ol> <li>If the list is empty, simply insert the elements of the tuple in the list and you are done</li> <li>Find the position of the a, b element in the list using binary search. (If the element does not exist, return the position of the least element greater than the searched for number)</li> <li>Let the positions returned be pos_a, pos_b for the tuple elements a, b respectively.</li> <li>If pos_a is even, list_A = [a]</li> <li>If pos_b is even, list_B = [b]</li> <li>The new list = rangeList[0:pos_a] + list_A + list_B + rangeList[b:end] and you are done.</li> </ol> <p>By converting the list of tuples to a single list, we eliminate the need for comparing the new elements with 2 different lists. Thus finding its position easily and by checking for odd or even, tells us whether it lies between an existing range</p> <pre><code>def subroutine(rangeList, currTuple) """ rangeList: Existing list of non overlapping tuples currTuple: The range tuple to be added """ if not rangeList: rangeList.extend(currTuple) else: a, b = binSearch(currTuple, rangeList) list_changed = rangeList[0:a] if a%2 == 0: list_changed.append(currTuple[0]) if b%2 == 0: list_changed.append(currTuple[1]) list_changed.extend(rangeList[b:]) return list_changed </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload