#!/usr/bin/env python

def coalesce_dates(dates):
    starts = [ date[0] for date in dates ]
    ends = [ date[1] for date in dates ]

    count = 0

    coalesced = []
    
    while starts:
        start = starts.pop
        count += 1
        

@@ -127,16 +127,54 @@
 dates %5D%0A
+    starts.sort()%0A    ends.sort()%0A    
 %0A    cou
@@ -249,16 +249,19 @@
 arts.pop
+(0)
 %0A       
@@ -277,12 +277,261 @@
  1%0A        %0A
+        while count %3E 0:%0A            if starts%5B0%5D %3C= ends%5B0%5D:%0A                starts.pop(0)%0A                count += 1%0A            else:%0A                end = ends.pop(0)%0A                count -= 1%0A            %0A        coalesced.append((start,end))%0A

Add coalesce algorithm.