Esempio n. 1
0
def list_interval_assignment():
    interval_list = []
    valid = False
    while valid is False:
        inputs = input('List of valid intervals? ')
        inputs = inputs.replace(' ','')
        try:
            assignments = list(inputs.split(sep=','))
            while (len(assignments) > 0):
                user_interval = assignments[:2] 
                assignments.pop(0)
                assignments.pop(0)
                if user_interval[0][0] is '[':
                    start = '['
                    lower = int(user_interval[0][1:])
                    lower_print = user_interval[0][1:]
                elif user_interval[0][0] is '(':
                    start = '('
                    lower = int(user_interval[0][1:]) + 1
                    lower_print = user_interval[0][1:]
                if user_interval[1][-1] is ']':
                    end = ']'
                    upper = int(user_interval[1][:-1])
                    upper_print = user_interval[1][:-1]
                    if upper >= lower:
                        add_interval = interval(start, lower_print, upper_print, end)
                        interval_list.append(add_interval)
                        valid = True
                    else:
                        print('Invalid Interval')
                elif user_interval[1][-1] is ')':
                    end = ')'
                    upper = int(user_interval[1][:-1]) - 1
                    upper_print = user_interval[1][:-1]
                    if upper >= lower:
                        add_interval = interval(start, lower_print, upper_print, end)
                        interval_list.append(add_interval)
                        valid = True
                    else:
                        print('Invalid Interval')
                        
# All invalid entries are handled here, except in the case of non-sensical bounds.
        except ValueError:
            print('Invalid Interval')
        except IndexError:
            print('Invalid Interval')
        except UnboundLocalError:
            print('Invalid Interval')
        except TypeError:
            print('Invalid Interval')
    
    return interval_list
Esempio n. 2
0
def list_interval_assignment():
    interval_list = []
    valid = False
    while valid is False:
        inputs = input("List of valid intervals? ")
        inputs = inputs.replace(" ", "")
        try:
            assignments = list(inputs.split(sep=","))
            while len(assignments) > 0:
                user_interval = assignments[:2]
                assignments.pop(0)
                assignments.pop(0)
                if user_interval[0][0] is "[":
                    start = "["
                    lower = int(user_interval[0][1:])
                    lower_print = user_interval[0][1:]
                elif user_interval[0][0] is "(":
                    start = "("
                    lower = int(user_interval[0][1:]) + 1
                    lower_print = user_interval[0][1:]
                if user_interval[1][-1] is "]":
                    end = "]"
                    upper = int(user_interval[1][:-1])
                    upper_print = user_interval[1][:-1]
                    if upper >= lower:
                        add_interval = interval(start, lower_print, upper_print, end)
                        interval_list.append(add_interval)
                        valid = True
                    else:
                        print("Invalid Interval")
                elif user_interval[1][-1] is ")":
                    end = ")"
                    upper = int(user_interval[1][:-1]) - 1
                    upper_print = user_interval[1][:-1]
                    if upper >= lower:
                        add_interval = interval(start, lower_print, upper_print, end)
                        interval_list.append(add_interval)
                        valid = True
                    else:
                        print("Invalid Interval")

        # All invalid entries are handled here, except in the case of non-sensical bounds.
        except ValueError:
            print("Invalid Interval")
        except IndexError:
            print("Invalid Interval")
        except UnboundLocalError:
            print("Invalid Interval")
        except TypeError:
            print("Invalid Interval")

    return interval_list
Esempio n. 3
0
def single_interval_assignment():
    single_interval_list = []
    valid = False
    while valid is False:
        inputs = input('Interval? ')
        if inputs == 'quit':
            return 0
            break
        inputs = inputs.replace(' ','')
        try:
            user_interval = inputs.split(sep=',')
            if user_interval[0][0] is '[':
                start = '['
                lower = int(user_interval[0][1:])
                lower_print = user_interval[0][1:]
            elif user_interval[0][0] is '(':
                start = '('
                lower = int(user_interval[0][1:]) + 1
                lower_print = user_interval[0][1:]
            if user_interval[1][-1] is ']':
                end = ']'
                upper = int(user_interval[1][:-1])
                upper_print = user_interval[1][:-1]
                if upper >= lower:
                    single_interval = interval(start, lower_print, upper_print, end)
                    single_interval_list.append(single_interval)
                    valid = True
                else:
                    print('Invalid Interval')
            elif user_interval[1][-1] is ')':
                end = ')'
                upper = int(user_interval[1][:-1]) - 1
                upper_print = user_interval[1][:-1]
                if upper >= lower:
                    valid = True
                    single_interval = interval(start, lower_print, upper_print, end)
                    single_interval_list.append(single_interval)
                else:
                    print('Invalid Interval')
        except ValueError:
            print('Invalid Interval')
        except IndexError:
            print('Invalid Interval')
        except UnboundLocalError:
            print('Invalid Interval')
        except TypeError:
            print('Invalid Interval')
    
    return single_interval_list
    
Esempio n. 4
0
def single_interval_assignment():
    single_interval_list = []
    valid = False
    while valid is False:
        inputs = input("Interval? ")
        if inputs == "quit":
            return 0
            break
        inputs = inputs.replace(" ", "")
        try:
            user_interval = inputs.split(sep=",")
            if user_interval[0][0] is "[":
                start = "["
                lower = int(user_interval[0][1:])
                lower_print = user_interval[0][1:]
            elif user_interval[0][0] is "(":
                start = "("
                lower = int(user_interval[0][1:]) + 1
                lower_print = user_interval[0][1:]
            if user_interval[1][-1] is "]":
                end = "]"
                upper = int(user_interval[1][:-1])
                upper_print = user_interval[1][:-1]
                if upper >= lower:
                    single_interval = interval(start, lower_print, upper_print, end)
                    single_interval_list.append(single_interval)
                    valid = True
                else:
                    print("Invalid Interval")
            elif user_interval[1][-1] is ")":
                end = ")"
                upper = int(user_interval[1][:-1]) - 1
                upper_print = user_interval[1][:-1]
                if upper >= lower:
                    valid = True
                    single_interval = interval(start, lower_print, upper_print, end)
                    single_interval_list.append(single_interval)
                else:
                    print("Invalid Interval")
        except ValueError:
            print("Invalid Interval")
        except IndexError:
            print("Invalid Interval")
        except UnboundLocalError:
            print("Invalid Interval")
        except TypeError:
            print("Invalid Interval")

    return single_interval_list
Esempio n. 5
0
def user_input_function():
    '''user_input_function() takes no arguments. Instead, it gets a
    keyboard input list of intervals from the caller. Then, it repeatedly
    gets a new interval from the caller and inserts this new interval
    into the list, finally printing the merged list to the screen. It
    quits upon keyboard input 'quit'.'''
    need_input = True
    while need_input:
        list_of_intervals = raw_input("List of intervals? ")
        if list_of_intervals == 'quit':
            return
        split_list = re.findall(r'[[(].*?[\])]', list_of_intervals)
        if len(split_list) == 0:
            print "No intervals in input list- try again"
            continue
        intervals = []
        need_input = False
        for input_interval in split_list:
            try:
                intervals.append(interval(input_interval))
            except:
                print "Invalid interval in list- try again"
                need_input = True
                break
            else:
                intervals.append(interval(input_interval))
        intervals = mergeOverlapping(intervals)

    while True:
        new_interval = raw_input("Interval? ")
        if new_interval == 'quit':
            return
        else:
            processed_interval = re.findall(r'[[(].*?[\])]', new_interval)
            try:
                processed_interval = interval(processed_interval[0])
            except:
                print "Invalid interval"
                continue
            else:
                intervals = insert(intervals, processed_interval)
                print intervals
Esempio n. 6
0
def user_input_function():
    '''user_input_function() takes no arguments. Instead, it gets a
    keyboard input list of intervals from the caller. Then, it repeatedly
    gets a new interval from the caller and inserts this new interval
    into the list, finally printing the merged list to the screen. It
    quits upon keyboard input 'quit'.'''
    need_input = True
    while need_input:
        list_of_intervals = raw_input("List of intervals? ")
        if list_of_intervals == 'quit':
            return
        split_list = re.findall(r'[[(].*?[\])]',list_of_intervals)
        if len(split_list) == 0:
            print "No intervals in input list- try again"
            continue
        intervals = []
        need_input = False
        for input_interval in split_list:
            try:
                intervals.append(interval(input_interval))
            except:
                print "Invalid interval in list- try again"
                need_input = True
                break
            else:
                intervals.append(interval(input_interval))
        intervals = mergeOverlapping(intervals)
    
    while True:
        new_interval = raw_input("Interval? ")
        if new_interval == 'quit':
            return
        else:
            processed_interval = re.findall(r'[[(].*?[\])]',new_interval)
            try:
                processed_interval = interval(processed_interval[0])
            except:
                print "Invalid interval"
                continue
            else:
                intervals = insert(intervals, processed_interval)
                print intervals
Esempio n. 7
0
def mergeIntervals(int1, int2):
    ''' mergeIntervals(int1, int2) takes two interval objects
    as arguments. If the two interval objects are adjacent or
    overlapping, it merges them and returns a single, merged
    interval object that containst the union of the two input
    integer intervals.'''
    if (len(set(int1.integers) & set(int2.integers)) > 0):
        if int1.lower_limit < int2.lower_limit:
            merged_interval = int1.lower_bound + str(int1.lower_limit) + ','
        elif int2.lower_limit < int1.lower_limit:
            merged_interval = int2.lower_bound + str(int2.lower_limit) + ','
        elif int1.lower_limit == int2.lower_limit:
            if (int1.lower_bound == '(' and int2.lower_bound == '('):
                merged_interval = '(' + str(int2.lower_limit) + ','
            else:
                merged_interval = '[' + str(int2.lower_limit) + ','

        if int1.upper_limit < int2.upper_limit:
            merged_interval = merged_interval + str(
                int2.upper_limit) + int2.upper_bound
        elif int2.upper_limit < int1.upper_limit:
            merged_interval = merged_interval + str(
                int1.upper_limit) + int1.upper_bound
        elif int1.upper_limit == int2.upper_limit:
            if (int1.upper_bound == ')' and int2.upper_bound == ')'):
                merged_interval = merged_interval + str(int2.upper_limit) + ')'
            else:
                merged_interval = merged_interval + str(int2.upper_limit) + ']'

    elif ((int1.lower_bound == '[' or int2.upper_bound == ']') and\
             (int1.lower_limit == int2.upper_limit)):
        merged_interval = int2.lower_bound + str(int2.lower_limit) + ',' + str(
            int1.upper_limit) + int1.upper_bound

    elif ((int1.upper_bound == ']' or int2.lower_bound == '[') and\
             (int1.upper_limit == int2.lower_limit)):
        merged_interval = int1.lower_bound + str(int1.lower_limit) + ',' + str(
            int2.upper_limit) + int2.upper_bound

    elif ((int1.lower_bound == '[' and int2.upper_bound == ']') and\
             (int1.lower_limit - 1 == int2.upper_limit)):
        merged_interval = int2.lower_bound + str(int2.lower_limit) + ',' + str(
            int1.upper_limit) + int1.upper_bound

    elif ((int1.upper_bound == ']' and int2.lower_bound == '[') and\
             (int1.upper_limit + 1 == int2.lower_limit)):
        merged_interval = int1.lower_bound + str(int1.lower_limit) + ',' + str(
            int2.upper_limit) + int2.upper_bound

    else:
        raise ValueError('The two input intervals do not overlap')

    return interval(merged_interval)
Esempio n. 8
0
def mergeIntervals(int1, int2):

    # Determines the lower bound and lower parameter of the merged interval, assuming overlapping does exist.
    if int1.lowerParameter > int2.lowerParameter:
        merged_lowerParameter = int1.lowerParameter
        merged_leftBound = int1.leftBound
    elif int1.lowerParameter < int2.lowerParameter:
        merged_lowerParameter = int2.lowerParameter
        merged_leftBound = int2.leftBound
    else:
        if int1.isLeftIncl == int2.isLeftIncl:
            merged_lowerParameter = int1.lowerParameter
            merged_leftBound = int1.leftBound
        else:
            merged_lowerParameter = int1.lowerParameter
            merged_leftBound = '('

    # Determines the upper bound and upper parameter of the merged interval, assuming overlapping does exist.
    if int1.upperParameter < int2.upperParameter:
        merged_upperParameter = int1.upperParameter
        merged_rightBound = int1.rightBound
    elif int1.upperParameter > int2.upperParameter:
        merged_upperParameter = int2.upperParameter
        merged_rightBound = int2.rightBound
    else:
        if int1.isRightIncl == int2.isRightIncl:
            merged_upperParameter = int1.upperParameter
            merged_rightBound = int1.rightBound
        else:
            merged_upperParameter = int1.upperParameter
            merged_leftBound = ')'

    # If the intervals cannot be merged, an exception should be thrown.
    if merged_lowerParameter > merged_upperParameter:
        raise ValueError(
            'There is no overlapping between these two intervals.')
    elif (merged_lowerParameter == merged_upperParameter) & (
        (not (merged_leftBound == '[')) | (not (merged_rightBound == ']'))):
        raise ValueError(
            'There is no overlapping between these two intervals.')
    elif (merged_lowerParameter == merged_upperParameter - 1) & (
        (merged_leftBound == '(') & (merged_rightBound == ')')):
        raise ValueError(
            'There is no overlapping between these two intervals.')

    else:
        merged_string = merged_leftBound + str(
            merged_lowerParameter) + "," + str(
                merged_upperParameter) + merged_rightBound
        merged_interval = interval(merged_string)

    return merged_interval
Esempio n. 9
0
def mergeIntervals(int1, int2):
    ''' mergeIntervals(int1, int2) takes two interval objects
    as arguments. If the two interval objects are adjacent or
    overlapping, it merges them and returns a single, merged
    interval object that containst the union of the two input
    integer intervals.'''
    if (len(set(int1.integers) & set(int2.integers)) > 0):
        if  int1.lower_limit < int2.lower_limit:
           merged_interval = int1.lower_bound + str(int1.lower_limit) + ','
        elif int2.lower_limit < int1.lower_limit:
            merged_interval = int2.lower_bound + str(int2.lower_limit) + ','
        elif int1.lower_limit == int2.lower_limit:
            if (int1.lower_bound == '(' and int2.lower_bound == '('):
                merged_interval = '(' + str(int2.lower_limit) + ','
            else: 
                merged_interval = '[' + str(int2.lower_limit) + ','
                
        if  int1.upper_limit < int2.upper_limit:
            merged_interval = merged_interval + str(int2.upper_limit) + int2.upper_bound
        elif int2.upper_limit < int1.upper_limit:
            merged_interval = merged_interval + str(int1.upper_limit) + int1.upper_bound
        elif int1.upper_limit == int2.upper_limit:
            if (int1.upper_bound == ')' and int2.upper_bound == ')'):
                merged_interval = merged_interval + str(int2.upper_limit) + ')'
            else: 
                merged_interval = merged_interval + str(int2.upper_limit) + ']'

    elif ((int1.lower_bound == '[' or int2.upper_bound == ']') and\
             (int1.lower_limit == int2.upper_limit)):
        merged_interval = int2.lower_bound + str(int2.lower_limit) + ',' + str(int1.upper_limit) + int1.upper_bound
    
    elif ((int1.upper_bound == ']' or int2.lower_bound == '[') and\
             (int1.upper_limit == int2.lower_limit)):
        merged_interval = int1.lower_bound + str(int1.lower_limit) + ',' + str(int2.upper_limit) + int2.upper_bound

    elif ((int1.lower_bound == '[' and int2.upper_bound == ']') and\
             (int1.lower_limit - 1 == int2.upper_limit)):
        merged_interval = int2.lower_bound + str(int2.lower_limit) + ',' + str(int1.upper_limit) + int1.upper_bound

    elif ((int1.upper_bound == ']' and int2.lower_bound == '[') and\
             (int1.upper_limit + 1 == int2.lower_limit)):
        merged_interval = int1.lower_bound + str(int1.lower_limit) + ',' + str(int2.upper_limit) + int2.upper_bound
        
    else:
        raise ValueError('The two input intervals do not overlap')
    
    return interval(merged_interval)
Esempio n. 10
0
def mergeIntervals(int1, int2):

   # Determines the lower bound and lower parameter of the merged interval, assuming overlapping does exist.
   if int1.lowerParameter > int2.lowerParameter:
      merged_lowerParameter = int1.lowerParameter
      merged_leftBound = int1.leftBound
   elif int1.lowerParameter < int2.lowerParameter:
      merged_lowerParameter = int2.lowerParameter
      merged_leftBound = int2.leftBound
   else:
      if int1.isLeftIncl == int2.isLeftIncl:
	 merged_lowerParameter = int1.lowerParameter
	 merged_leftBound = int1.leftBound
      else:
         merged_lowerParameter = int1.lowerParameter
         merged_leftBound = '('


    # Determines the upper bound and upper parameter of the merged interval, assuming overlapping does exist.
   if int1.upperParameter < int2.upperParameter:
      merged_upperParameter = int1.upperParameter
      merged_rightBound = int1.rightBound
   elif int1.upperParameter > int2.upperParameter:
      merged_upperParameter = int2.upperParameter
      merged_rightBound = int2.rightBound
   else:
      if int1.isRightIncl == int2.isRightIncl:
	 merged_upperParameter = int1.upperParameter
	 merged_rightBound = int1.rightBound
      else:
         merged_upperParameter = int1.upperParameter
         merged_leftBound = ')'


   # If the intervals cannot be merged, an exception should be thrown.
   if merged_lowerParameter > merged_upperParameter:
      raise ValueError('There is no overlapping between these two intervals.')
   elif (merged_lowerParameter == merged_upperParameter) & ((not(merged_leftBound == '[')) | (not(merged_rightBound == ']'))):
      raise ValueError('There is no overlapping between these two intervals.')
   elif (merged_lowerParameter == merged_upperParameter - 1) & ((merged_leftBound == '(') & (merged_rightBound == ')')):
      raise ValueError('There is no overlapping between these two intervals.')

   else: 
      merged_string = merged_leftBound + str(merged_lowerParameter) + "," + str(merged_upperParameter) + merged_rightBound
      merged_interval = interval(merged_string)

   return merged_interval
Esempio n. 11
0
def mergeIntervals(int1, int2):
    # The merge function first takes into account what the bounds of each interval are.
    if type(int1.intRange()) is int:
        max1 = int1.intRange()
        min1 = int1.intRange()
    elif type(int1.intRange()) is tuple:
        max1 = max(int1.intRange())
        min1 = min(int1.intRange())
    if type(int2.intRange()) is int:
        max2 = int2.intRange()
        min2 = int2.intRange()
    elif type(int2.intRange()) is tuple:
        max2 = max(int2.intRange())
        min2 = min(int2.intRange())


# After determining the bounds, the new interval is created.
    if (max1 + 1) >= min2 and max1 <= max2 and min1 <= min2:
        start = int1.start
        low = int1.lower_print
        up = int2.upper_print
        end = int2.end
    elif (max2 + 1) >= min1 and max2 <= max1 and min2 <= min1:
        start = int2.start
        low = int2.lower_print
        up = int1.upper_print
        end = int1.end
    elif (max1 + 1) >= min2 and max1 <= max2 and min1 >= min2:
        start = int2.start
        low = int2.lower_print
        up = int2.upper_print
        end = int2.end
    elif (max2 + 1) >= min1 and max2 <= max1 and min2 >= min1:
        start = int1.start
        low = int1.lower_print
        up = int1.upper_print
        end = int1.end
    else:
        raise mergeException()

    newInterval = interval(start, low, up, end)
    return newInterval
Esempio n. 12
0
def mergeIntervals(int1, int2):
# The merge function first takes into account what the bounds of each interval are.
    if type(int1.intRange()) is int:
        max1 = int1.intRange()
        min1 = int1.intRange()
    elif type(int1.intRange()) is tuple:
        max1 = max(int1.intRange())
        min1 = min(int1.intRange())
    if type(int2.intRange()) is int:
        max2 = int2.intRange()
        min2 = int2.intRange()
    elif type(int2.intRange()) is tuple:
        max2 = max(int2.intRange())
        min2 = min(int2.intRange())

# After determining the bounds, the new interval is created.        
    if (max1 + 1) >= min2 and max1 <= max2 and min1 <= min2:
        start = int1.start
        low   = int1.lower_print
        up    = int2.upper_print
        end   = int2.end
    elif (max2 + 1) >= min1 and max2 <= max1 and min2 <= min1:
        start = int2.start
        low   = int2.lower_print
        up    = int1.upper_print
        end   = int1.end
    elif (max1 + 1) >= min2 and max1 <= max2 and min1 >= min2:
        start = int2.start
        low   = int2.lower_print
        up    = int2.upper_print
        end   = int2.end
    elif (max2 + 1) >= min1 and max2 <= max1 and min2 >= min1:
        start = int1.start
        low   = int1.lower_print
        up    = int1.upper_print
        end   = int1.end
    else:
        raise mergeException()
    
    newInterval = interval(start, low, up, end)
    return newInterval
Esempio n. 13
0
 def test_insert(self):
     insert_test_list = [interval('[','1','3',')'), interval('[','7','9',')')]
     insert_test_addition = [interval('[','4','6',']')]
     insert_test_results = [interval('[','1','9',')')]
     
     self.assertEqual(insert(insert_test_list,insert_test_addition),insert_test_result)
Esempio n. 14
0
 def test_merge_overlapping(self):
     merge_test_list = [interval('[','1','5',')'), interval('[','3','9',')')]
     merge_test_result = [interval('[','1','9',')')]
     self.assertEqual(merge_test_list, merge_test_result)
Esempio n. 15
0
 def test_merge_interval(self):
     interval1 = interval('[','1','5',')')
     interval2 = interval('[','3','9',')')
     interval_result = [interval('[','1','9',')')]
     self.assertEqual(mergeIntervals(interval1, interval2), interval_result)
Esempio n. 16
0
 def test_class(self):
     interval_test = interval('[','3','9',')')
     interval_result = interval('[','3','9',')')
     self.assertEqual(interval_test, interval_result)
Esempio n. 17
0
from interval_class import interval
from functions import *
import operator

interval_list = []

while True:
    if len(interval_list) == 0:
        input_string = raw_input('List of Intervals?')
        if input_string == 'quit':
            break
        input_string = input_string.replace(' ', '')
        input_split = input_string.split(',')
        try:
            for i in range(0, len(input_split), 2):
                int_i = interval(input_split[i] + ',' + input_split[i + 1])
                interval_list.append(int_i)
        except:
            print('Invalid intervals')
        interval_list = mergeOverlapping(interval_list)

    else:
        input_string = raw_input('Interval?')

        if input_string == 'quit':
            break

        input_string = input_string.replace(' ', '')

        try:
            int_new = interval(input_string)
Esempio n. 18
0
from functions import *
import operator


interval_list = []

while True:
   if len(interval_list) == 0:
      input_string = raw_input('List of Intervals?')
      if input_string == 'quit':
         break
      input_string = input_string.replace(' ', '')
      input_split = input_string.split(',')
      try:
         for i in range(0, len(input_split), 2):
            int_i = interval(input_split[i] + ',' + input_split[i+1])
            interval_list.append(int_i)
      except:
         print('Invalid intervals') 
      interval_list = mergeOverlapping(interval_list)
   


   else:
      input_string = raw_input('Interval?')

      if input_string == 'quit':
         break
      
      input_string = input_string.replace(' ', '')