I have 2 lists of ordered times which are the start/stop times for ServiceA and ServiceB, respectively. I want to combine the lists in one list containing the start and stop times (in order) of when at least 1 of the services was running. (Both services always start after 00:01:00 and stop before 23:59:00.)
Example:
ListA = ["08:03:19","14:22:22","17:00:02","18:30:01"]
ListB = ["15:19:03","18:00:00","18:35:05","19:01:00"]
... the magic happens
Result =["08:03:19","14:22:22","15:19:03","18:30:01","18:35:05","19:01:00"]
The code below does not produce the desired results. After numerous attempts that accounted for most but not all possible cases, I'm posting what I have currently. The lists could be vastly different, for example there may be no overlap or complete overlap between start/stop times of the 2 services.
#!/usr/bin/python2.7
def combineOverlappingTimes(aList, bList, CurrentResults):
ReturnList = []
aStart = aList[0]
aStop = aList[1]
bStart = bList[0]
bStop = bList[1]
if len(CurrentResults) == 0:
LastTimeInCurrentResults = "00:00:00"
else:
LastTimeInCurrentResults = CurrentResults[(len(CurrentResults)-1)]
print "aStart= %s\naStop= %s\nbStart= %s\nbStop= %s" % (aStart,aStop,bStart,bStop)
print "LastTimeInCurrentResults= %s" % LastTimeInCurrentResults
if aStart >= LastTimeInCurrentResults and bStart >= LastTimeInCurrentResults:
if aStart > bStart:
if bStart > aStop:
ReturnList.append( (aStart,aStop) )
elif bStart < aStop:
ReturnList.append( (bStart,bStop ) )
else: #(aStart < bStart)
if aStop < bStart:
ReturnList.append( (bStart,bStop) )
elif aStop > bStop:
ReturnList.append( (bStart,aStop) )
elif aStart >= LastTimeInCurrentResults:
ReturnList.append( (aStart, aStop) )
else: # either A or B is beforeLastTime
if aStart < LastTimeInCurrentResults:
ReturnList.append( (LastTimeInCurrentResults, aStop) )
elif bStart < LastTimeInCurrentResults:
ReturnList.append( (LastTimeInCurrentResults, bStop) )
print ( "combineOverlappingTime ReturnList= " + str(ReturnList))
print "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n"
return ReturnList
# main()
#####################################################################
def main():
ListA = ["08:03:19","14:22:22","14:22:25","14:22:30","18:00:02","18:30:01"]
ListB = ["14:22:36","15:18:10","15:19:03","18:00:01","18:00:05","19:01:00"]
ResultList = []
i = 0
while i < len(ListA):
if i == 0:
ListA_StartTime= ListA[i]
ListA_StopTime = ListA[i+1]
else:
if i == len(ListA)-2:
ListA_StartTime= ListA[i]
ListA_StopTime = ListA[i+1]
else:
ListA_StartTime= ListA[i]
ListA_StopTime = ListA[i+1]
j = 0
ListB_StartTime, ListB_StopTime = "",""
for time in ListB:
if j % 2 == 0:
ListB_StartTime= time
else:
ListB_StopTime = time
if ListB_StartTime!= "" and ListB_StopTime != "":
tempSetA, tempSetB = [], []
tempSetA.append(ListB_StartTime)
tempSetA.append(ListB_StopTime)
tempSetB.append(ListA_StartTime)
tempSetB.append(ListA_StopTime)
combinedTimes = combineOverlappingTimes(tempSetA, tempSetB, ResultList)
for start,stop in combinedTimes:
ResultList.append(start)
ResultList.append(stop)
ListB_StartTime, ListB_StopTime = "",""
j += 1
i += 2
print "ResultList= %s \n\n" % str(ResultList)
DesiredList = ["08:03:19","14:22:22","14:22:25","14:22:30","14:22:36","15:18:10","15:19:03","18:00:01","18:00:02","19:01:00"]
print "Desired Results: %s" % str(DesiredList)
if __name__ == '__main__':
main()