def test_InsertInt(self): self.assertEqual( interval.to_string( interval.insert( [interval('[1,3]'), interval('[6,9]')], interval('[2,5]'))), '[1,9]') self.assertEqual( interval.to_string( interval.insert([ interval('[1,2]'), interval('(3,5)'), interval('[6,7)'), interval('(8,10]'), interval('[12,16]') ], interval('[4,9]'))), '[1,2], (3,10], [12,16]')
def test_MergeOvp(self): self.assertEqual( interval.to_string( interval.mergeOverlapping([ interval('[1,5]'), interval('[2,6)'), interval('(8,10]'), interval('[8,18]') ])), '[1,5], [8,18]') self.assertEqual( interval.to_string( interval.mergeOverlapping([ interval('[-10,-7]'), interval('(-4,1]'), interval('[3,12)'), interval('[15,23]'), interval('[24,24]') ])), '[-10,-7], (-4,1], [3,12), [15,24]')
def test_Merge(self): self.assertEqual( interval.to_string([ interval.mergeIntervals(interval('[1,3]'), interval('[3,5]')) ]), '[1,5]') self.assertEqual( interval.to_string([ interval.mergeIntervals(interval('[-1,4]'), interval('[2,3]')) ]), '[-1,4]') self.assertEqual( interval.to_string([ interval.mergeIntervals(interval('[2,3]'), interval('[1,2]')) ]), '[1,3]') self.assertEqual( interval.to_string([ interval.mergeIntervals(interval('[1,5]'), interval('[6,9]')) ]), '[1,9]') self.assertEqual( interval.to_string([ interval.mergeIntervals(interval('[1,23]'), interval('[24,24]')) ]), '[1,24]')
from intervalclass import interval if __name__ == "__main__": while True: try: input_string = input("List of intervals? ") if input_string == "quit": sys.exit(1) input_string = input_string.replace(" ", "") string_list = input_string.split(",") list_interval = [] for i in range(0, len(string_list), 2): list_interval.append( interval(string_list[i] + ',' + string_list[i + 1])) list_interval = interval.mergeOverlapping(list_interval) print(interval.to_string(list_interval)) break except KeyboardInterrupt: sys.exit(1) except Exception: print('Invalid list of intervals') pass while True: try: inp = input("Interval? ") if inp == "quit": sys.exit(1) break else: inp = interval(inp) list_interval = interval.insert(list_interval, inp)
import sys from intervalclass import interval if __name__=="__main__": while True: try: input_string = input("List of intervals? ") if input_string == "quit": sys.exit(1) input_string = input_string.replace(" ","") string_list = input_string.split(",") list_interval=[] for i in range(0,len(string_list),2): list_interval.append(interval(string_list[i]+','+string_list[i+1])) list_interval = interval.mergeOverlapping(list_interval) print (interval.to_string(list_interval)) break except KeyboardInterrupt: sys.exit(1) except Exception: print ('Invalid list of intervals') pass while True: try: inp = input("Interval? ") if inp =="quit": sys.exit(1) break else: inp = interval(inp) list_interval = interval.insert(list_interval,inp)
def test_InsertInt(self): self.assertEqual(interval.to_string(interval.insert([interval('[1,3]'),interval('[6,9]')], interval('[2,5]'))), '[1,9]') self.assertEqual(interval.to_string(interval.insert([interval('[1,2]'),interval('(3,5)'),interval('[6,7)'),interval('(8,10]'),interval('[12,16]')], interval('[4,9]'))), '[1,2], (3,10], [12,16]')
def test_MergeOvp(self): self.assertEqual(interval.to_string(interval.mergeOverlapping([interval('[1,5]'),interval('[2,6)'),interval('(8,10]'),interval('[8,18]')])), '[1,5], [8,18]') self.assertEqual(interval.to_string(interval.mergeOverlapping([interval('[-10,-7]'),interval('(-4,1]'),interval('[3,12)'),interval('[15,23]'),interval('[24,24]')])), '[-10,-7], (-4,1], [3,12), [15,24]')
def test_Merge(self): self.assertEqual(interval.to_string([interval.mergeIntervals(interval('[1,3]'), interval('[3,5]'))]),'[1,5]') self.assertEqual(interval.to_string([interval.mergeIntervals(interval('[-1,4]'), interval('[2,3]'))]),'[-1,4]') self.assertEqual(interval.to_string([interval.mergeIntervals(interval('[2,3]'), interval('[1,2]'))]),'[1,3]') self.assertEqual(interval.to_string([interval.mergeIntervals(interval('[1,5]'), interval('[6,9]'))]),'[1,9]') self.assertEqual(interval.to_string([interval.mergeIntervals(interval('[1,23]'), interval('[24,24]'))]),'[1,24]')