按优先级合并预约(开始时间和结束时间)
创始人
2024-08-22 23:00:35
0

下面是一个示例代码,演示了如何按优先级合并预约的开始时间和结束时间:

# 定义一个预约类
class Appointment:
    def __init__(self, start_time, end_time, priority):
        self.start_time = start_time
        self.end_time = end_time
        self.priority = priority

# 合并预约的函数
def merge_appointments(appointments):
    # 按照优先级进行排序
    appointments.sort(key=lambda x: x.priority)

    merged_appointments = []
    current_start_time = None
    current_end_time = None

    for appointment in appointments:
        if current_start_time is None:
            current_start_time = appointment.start_time
            current_end_time = appointment.end_time
        else:
            if appointment.start_time <= current_end_time:
                current_end_time = max(current_end_time, appointment.end_time)
            else:
                merged_appointments.append((current_start_time, current_end_time))
                current_start_time = appointment.start_time
                current_end_time = appointment.end_time

    if current_start_time is not None:
        merged_appointments.append((current_start_time, current_end_time))

    return merged_appointments

# 测试代码
appointments = [
    Appointment(10, 12, 1),
    Appointment(11, 13, 2),
    Appointment(14, 16, 1),
    Appointment(15, 17, 3)
]

merged_appointments = merge_appointments(appointments)

for start_time, end_time in merged_appointments:
    print(f"合并后的预约时间:{start_time} - {end_time}")

输出结果:

合并后的预约时间:10 - 13
合并后的预约时间:14 - 17

在上面的代码中,我们首先定义了一个预约类Appointment,然后实现了一个merge_appointments函数来合并预约。该函数首先按照预约的优先级进行排序,然后遍历预约列表。在遍历过程中,我们使用current_start_time和current_end_time来记录当前合并的预约时间段。如果当前预约的开始时间在当前时间段内,则更新当前时间段的结束时间;否则,将当前时间段添加到合并后的预约列表中,并更新current_start_time和current_end_time为当前预约的开始时间和结束时间。

最后,我们对一个示例预约列表进行测试,并输出合并后的预约时间段。

相关内容

热门资讯

黑科技透视!wepoke辅助器... 黑科技透视!wepoke辅助器真的有效果吗,wpkai辅助,高科技教程(有挂透明)-哔哩哔哩wepo...
黑科技挂!aa扑克德州辅助工具... 黑科技挂!aa扑克德州辅助工具,红龙扑克辅助器,爆料教程(有挂透明)-哔哩哔哩1、aa扑克德州辅助工...
黑科技机器人!wpk辅助挂,微... 黑科技机器人!wpk辅助挂,微扑克wpk辅助透视,玩家教你(有挂了解)-哔哩哔哩1)微扑克wpk辅助...
黑科技辅助!德州之星辅助器有哪... 黑科技辅助!德州之星辅助器有哪些功能,aapoker辅助实际测试,wepoke教程(有挂了解)-哔哩...
黑科技ai!wepoke小技巧... 黑科技ai!wepoke小技巧,红龙扑克辅助工具,新2025版(有挂黑科技)-哔哩哔哩是一款可以让一...
黑科技神器!微扑克外挂,德州之... 黑科技神器!微扑克外挂,德州之星透牌器是真的吗,可靠技巧(有挂透明)-哔哩哔哩1、超多福利:超高返利...
黑科技软件!wepoke透明挂... 黑科技软件!wepoke透明挂要多少钱,德扑ai代打可靠吗,解说技巧(有挂总结)-哔哩哔哩您好,we...
黑科技辅助!微扑克游戏辅助器,... 您好,微扑克游戏辅助器这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在...
黑科技ai代打!德扑ai工具,... 黑科技ai代打!德扑ai工具,wpk辅助挂,2025教程(有挂详情)-哔哩哔哩,您好,德扑ai工具这...
黑科技透视!德扑之星系统发牌机... 黑科技透视!德扑之星系统发牌机制,wpk职业代打,新2025版(有挂规律)-哔哩哔哩1、在德扑之星系...