#NEWLINE# def normalize_slots( slots ) : #NEWLINE# #INDENT# 'Slot manipulation library\n    This function "normalizes" a list of slots by merging all consecutive or\n    overlapping slots into non-overlapping ones. IMPORTANT: the input slots\n    array has to be sorted by initial datetime of the slot, this is, for a\n    given slot at position \'i\', it is always met the following condition:\n    slots[i][\'starting_date\'] <= slots[i+1][\'starting_date\'].\n    :param slots: The list of slots to be normalized.\n    :return: The normalized list of slots.\n    ' #NEWLINE# if ( slots is None ) : #NEWLINE# #INDENT# return [ ] #NEWLINE# #UNINDENT# slots_n = len ( slots ) #NEWLINE# if ( slots_n < 2 ) : #NEWLINE# #INDENT# return slots #NEWLINE# #UNINDENT# normalized_s = [ ] #NEWLINE# s = s_i [ 0 ] #NEWLINE# t = slots [ 1 ] #NEWLINE# s_i = 0 #NEWLINE# t_i = 1 #NEWLINE# while ( s_i < slots_n ) : #NEWLINE# #INDENT# if ( s [ 1 ] < t [ 0 ] ) : #NEWLINE# #INDENT# normalized_s . append ( s ) #NEWLINE# s = t #NEWLINE# #UNINDENT# if ( ( s [ 0 ] <= t [ 0 ] ) and ( s [ 1 ] <= t [ 1 ] ) ) : #NEWLINE# #INDENT# s = ( s [ 0 ] , t [ 1 ] ) #NEWLINE# #UNINDENT# if ( t_i < ( slots_n - 1 ) ) : #NEWLINE# #INDENT# t_i += 1 #NEWLINE# t = slots [ t_i ] #NEWLINE# continue #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# normalized_s . append ( s ) #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# return normalized_s