Resolve schedule conflicts using python

Abdulaziz Alghofaily :

so I have the input of n users schedules, I want to form a new schedule that shows times Union

this is the input

['08:00-08:50', '09:00-09:50', '10:00-10:50', '11:00-11:50', '11:00-12:15', '12:00-12:50', '12:00-13:15', '13:00-13:50', '14:00-15:15', '15:30-18:10']

The output should be like this

['08:00-08:50', '09:00-09:50', '10:00-10:50', '11:00-13:50', '14:00-15:15', '15:30-18:10']

That what i have tried so far

if start_currenttime < start_oldtime and end_currenttime >= start_oldtime:
    start=start_currenttime
    flag=1

    if end_currenttime>end_oldtime:
        end=end_currenttime

if start_currenttime >= start_oldtime and start_currenttime <= end_oldtime and end_currenttime > end_oldtime:
    flag=1
    end=end_currenttime

if flag:
     return str(start)+"-"+str(end)
else: return "-1"
ncica :

This is one way how you can do it, with basic for loop:

data = ['08:00-08:50', '09:00-09:50', '10:00-10:50', '11:00-11:50', '11:00-12:15', '12:00-12:50', '12:00-13:15', '13:00-13:50', '14:00-15:15', '15:30-18:10']
data_ = [interval.split('-') for interval in data]


interval_idx = 0
for i in data_:
    if i[0] > data_[interval_idx][1]:
        interval_idx += 1
        data_[interval_idx] = i
    else:
        data_[interval_idx] = [data_[interval_idx][0], i[1]]

data_=['-'.join(interval) for interval in data_[:interval_idx+1]]

print(data_)

output:

['08:00-08:50', '09:00-09:50', '10:00-10:50', '11:00-13:50', '14:00-15:15', '15:30-18:10']

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=10207&siteId=1