0
def mergeoverlapping(initialranges):
    i = sorted(set([tuple(sorted(x)) for x in initialranges]))

    f = [i[0]]
    for c, d in i[1:]:
        a, b = f[-1]
        if c<=b<d:
            f[-1] = a, d
        elif b<c<d:
            f.append((c,d))
        else:
            pass
    return f

def main():
    #open file for reading
    list_of_Tups = []
    with open("intervals.txt") as in_file:
        for line in in_file:
            int_list = [int(i) for i in line.split()]
            line = int_list
            list_of_Tups.append(line)
            list_of_Tups.sort()

    answer = list(mergeoverlapping(list_of_Tups))
    print("Non-intersecting Intervals:")
    for i in range (len(answer)):
        print(answer[i])

main()

Given a data file, I had to create tuples, store tuples in list, sort the list and replace each pair of overlapping tuples with a single tuple.Then print list of non intersecting tuples.

But now I want to know how to print the non-intersecting intervals in increasing order of the size of the intervals. If two intervals are of the same size then print the two intervals in ascending order of their lower ends. So the output would look like:

Non-intersecting Intervals: (-4,3) (4,7) (10,15)

Non-intersecting Intervals in order of size: (4, 7) (10, 15) (-4, 3)

vallentin
  • 23,478
  • 6
  • 59
  • 81
  • Possible duplicate of [Merging Overlapping Intervals python](http://stackoverflow.com/questions/43600878/merging-overlapping-intervals-python) – vallentin Apr 26 '17 at 01:15
  • Interesting. It's even the same intervals as in a question from about a day ago. – vallentin Apr 26 '17 at 01:15
  • The first part of the problem is the same. But I want to know how to put the non-intersecting intervals in order of size. Do I take the out_put I already have and then create separate lists for each range, count the length of the list. and then print that in increasing order? – BingBing23 Apr 26 '17 at 01:20
  • I see you changed the intervals. But isn't the sorted order incorrect now? Wouldn't it be `(4, 7), (10, 15), (-4, 3)` which is correctly sorted? Since you said ascending order based on the size. – vallentin Apr 26 '17 at 01:29
  • Yes, my bad I flipped it around. – BingBing23 Apr 26 '17 at 01:34

1 Answers1

0

Given:

intervals = [(-25, -14), (-10, -3), (2, 6), (12, 18), (22, 30)]

Then like you've already been sorting, you just sort it based on the size.

# First sort in ascending order in relation to lower
intervals.sort(key=lambda interval: interval[0])

# Then sort in ascending order in relation to size
intervals.sort(key=lambda interval: abs(interval[0] - interval[1]))

Then print(intervals) outputs:

[(2, 6), (12, 18), (-10, -3), (22, 30), (-25, -14)]

Thus given (-4, 3), (4, 7), (-2, 1) then it results in (-2, 1), (4, 7), (-4, 3) (size is 3, 3, 7). So it correctly sorts based on lower and then size.

vallentin
  • 23,478
  • 6
  • 59
  • 81
  • I can't get it to print with the correct format now. – BingBing23 Apr 26 '17 at 01:42
  • Becuase I used a for loop to print the merged intervals, whenever I try to print the ordered intervals too I get inconsistent tab error – BingBing23 Apr 26 '17 at 01:43
  • That's not really a descriptive problem. You're of course supposed to use the above in place of your `mergeoverlapping` function. – vallentin Apr 26 '17 at 01:43
  • [For the tab error, you're mixing tabs and spaces.](http://stackoverflow.com/questions/28325553/tab-error-in-python) – vallentin Apr 26 '17 at 01:44