def test_merge_overlap(self): self.assertEqual( interval.mergeOverlapping([ interval("[1,5]"), interval("[2,6)"), interval("(8,10]"), interval("[8,18]") ]), "[1,5], [8,18]")
def test_interval(self): self.assertEqual(interval("[1,4]").lower,1) self.assertEqual(interval("[1,4]").upper,4) self.assertEqual(interval("(2,5]").lower,3) self.assertEqual(interval("(2,5]").upper,5) self.assertEqual(interval("[4,8)").lower,4) self.assertEqual(interval("[4,8)").upper,7) self.assertEqual(interval("(3,9)").lower,4) self.assertEqual(interval("(3,9)").upper,8)
def test_interval(self): self.assertEqual(interval("[1,4]").lower, 1) self.assertEqual(interval("[1,4]").upper, 4) self.assertEqual(interval("(2,5]").lower, 3) self.assertEqual(interval("(2,5]").upper, 5) self.assertEqual(interval("[4,8)").lower, 4) self.assertEqual(interval("[4,8)").upper, 7) self.assertEqual(interval("(3,9)").lower, 4) self.assertEqual(interval("(3,9)").upper, 8)
def test_equal_class_interval(self): self.assertEqual(str(interval('a')), "Invalid interval") self.assertEqual(str(interval('!*')),"Invalid interval") self.assertEqual(str(interval('(1,')),"Invalid interval") self.assertEqual(str(interval('(1,2)')),"Invalid interval") self.assertEqual(str(interval('(1,3)')),"(1,3) represents the numbers 2 through 2") self.assertEqual(str(interval('[1,3)')),"[1,3) represents the numbers 1 through 2") self.assertEqual(str(interval('(1,3]')),"(1,3] represents the numbers 2 through 3") self.assertEqual(str(interval('[1,3]')),"[1,3] represents the numbers 1 through 3")
def test_insert(self): self.assertEqual( interval.insert( [interval("[1,3]"), interval("[6,9]")], interval("[2,5]")), "[1,9]") self.assertEqual( 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_equal_class_interval(self): self.assertEqual(str(interval('a')), "Invalid interval") self.assertEqual(str(interval('!*')), "Invalid interval") self.assertEqual(str(interval('(1,')), "Invalid interval") self.assertEqual(str(interval('(1,2)')), "Invalid interval") self.assertEqual(str(interval('(1,3)')), "(1,3) represents the numbers 2 through 2") self.assertEqual(str(interval('[1,3)')), "[1,3) represents the numbers 1 through 2") self.assertEqual(str(interval('(1,3]')), "(1,3] represents the numbers 2 through 3") self.assertEqual(str(interval('[1,3]')), "[1,3] represents the numbers 1 through 3")
def test_merge(self): self.assertEqual( interval.mergeIntervals(interval("[1,3]"), interval("[3,5]")), "[1,5]") self.assertEqual( interval.mergeIntervals(interval("[-1,4]"), interval("[2,3]")), "[-1,4]") self.assertEqual( interval.mergeIntervals(interval("[1,5]"), interval("[6,9]")), "[1,9]")
def inpt(): user_input = input('List of intervals? Or enter quit to quit\n') flag=1 # Given an indicator for input, 0 means incorrect input, 1 means correct #If user doesn't input quit, we first convert the string input to what we need #Then, check whether the input intervals are correct #If the input is incorrect, break the for loop and then go to the next while loop if user_input != 'quit': convert_input = user_input_str(user_input) for i in range(len(convert_input)): intel = convert_input[i] if str(interval(intel)) == "Invalid interval": flag = 0 break #This while loop will check whether the user input is correct again until the user enter the correct intervals list while user_input != 'quit' and flag == 0: user_input = input('You have entered wrong interval list. Please input again or enter quit to quit\n') if user_input != 'quit': convert_input = user_input_str(user_input) for i in range(len(convert_input)): intel = convert_input[i] if str(interval(intel)) == "Invalid interval": flag = 0 break else: flag = 1 #### Part 2 input one more interval to merge with given intervals list #While the user input the correct intervals list #He/she will be asked to add one more interval again and agian until input quit if user_input != 'quit': merge_li = convert_input #Geting the intervals list from part 1 user_input = input('Interval? Add one more interval or enter quit to quit\n') while user_input != 'quit': one_int = user_input check_int = interval(one_int) #Given user input is not quit, the user will be asked to enter the interval until correct #Or user can enter quit to quit while str(check_int) == 'Invalid interval': print ('Invalid interval') user_input = input('Interval? Add one more interval or enter quit to quit\n') if user_input == "quit": break else: one_int=user_input check_int = interval(one_int) #If user entered the correct interval, #the the interval will be merged to the given intervals list #and then ask to enter another interval if str(check_int) != 'Invalid interval': merge_li = insert(merge_li, one_int) #since the new merge_li using list to present inclusive for each interval, #we need to convert back to string initial = [] for li in merge_li: initial.append(str(li)) merge_li = initial print (",".join(merge_li)) user_input = input('Interval? Input one more interval or enter quit to quit\n')
def test_insert(self): self.assertEqual(interval.insert([interval("[1,3]"),interval("[6,9]")], interval("[2,5]")), "[1,9]") self.assertEqual(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_merge_overlap(self): self.assertEqual(interval.mergeOverlapping([interval("[1,5]"),interval("[2,6)"),interval("(8,10]"),interval("[8,18]")]), "[1,5], [8,18]")
def test_merge(self): self.assertEqual(interval.mergeIntervals(interval("[1,3]"), interval("[3,5]")),"[1,5]") self.assertEqual(interval.mergeIntervals(interval("[-1,4]"), interval("[2,3]")),"[-1,4]") self.assertEqual(interval.mergeIntervals(interval("[1,5]"), interval("[6,9]")),"[1,9]")
import sys from class_interval import interval if __name__ == "__main__": while True: try: user_input = input("List of intervals? ") user_input = user_input.replace(" ", "") interval_list = user_input.split(",") empty_list_interval = [] for i in range(0, len(interval_list), 2): empty_list_interval.append( interval(interval_list[i] + "," + interval_list[i + 1])) new_list_interval = empty_list_interval print(new_list_interval) break except KeyboardInterrupt: sys.exit(1) except Exception: print("Invalid list of intervals") pass while True: try: input_interval = input("Interval?") if input_interval == "quit": sys.exit(1)
''' import sys from class_interval import interval if __name__=="__main__": while True: try: user_input = input("List of intervals? ") user_input = user_input.replace(" ","") interval_list = user_input.split(",") empty_list_interval=[] for i in range(0, len(interval_list), 2): empty_list_interval.append(interval(interval_list[i] + "," + interval_list[i+1])) new_list_interval = empty_list_interval print (new_list_interval) break except KeyboardInterrupt: sys.exit(1) except Exception: print ("Invalid list of intervals") pass while True: try: input_interval = input("Interval?") if input_interval == "quit": sys.exit(1)