", " ( OP ):
def compress_task_list(tasks):
tasks = list(tasks)
tasks.sort(key=lambda item: item[0])
result = []
first_start = tasks[0][0]
final_stop = tasks[0][1]
for start, stop in tasks[1:]:
if start > final_stop:
result.append((first_start, final_stop))
first_start = start
final_stop = stop
elif stop > final_stop:
final_stop = stop
result.append((first_start, final_stop))
return tuple(result)
if __name__ == '__main__':
import unittest
class Test_Compress_Task_List(unittest.TestCase):
def test_01(self):
"completely separate"
initial = ((8.0, 9.5), (10.0, 12.0), (13.0, 15.5), (16.0, 17.0))
expected = ((8.0, 9.5), (10.0, 12.0), (13.0, 15.5), (16.0, 17.0))
self.assertEqual(compress_task_list(initial), expected)
def test_02(self):
"end equals start"
initial = ((8.0, 9.5), (9.5, 12.0), (13.0, 15.5), (15.5, 17.0))
expected = ((8.0, 12.0), (13.0, 17.0))
self.assertEqual(compress_task_list(initial), expected)
def test_03(self):
"end equals start (with more empty times)"
initial = ((8.0, 8.5), (8.5, 10.0), (10.25, 12.0), (12.5, 13.75), (13.75, 15.0), (15.25, 16.0), (16.0, 17.0))
expected = ((8.0, 10.0), (10.25, 12.0), (12.5, 15.0), (15.25, 17.0))
self.assertEqual(compress_task_list(initial), expected)
def test_04(self):
"out of order, cross-overs, and tasks completely inside other tasks"
initial = ((8.0, 8.5), (8.0, 10.0), (10.25, 12.0), (10.0, 11.5), (13.0, 15.5), (14.0, 15.0), (16.0, 17.0))
expected = ((8.0, 12.0), (13.0, 15.5), (16.0, 17.0))
self.assertEqual(compress_task_list(initial), expected)
unittest.main()
, Python .;)