按优先级合并预约(开始时间和结束时间)
创始人
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软件透明挂,哈糖大菠萝切牌规律-好像真的有挂(攻略方法)1、这是跨平台的哈糖...
黑科技辅助!德州wepower... 黑科技辅助!德州wepower软件透明挂,德扑之星可以查数据-一般真的有挂(扑克教程);无聊就玩这款...
wepoke辅助!wePokE... wepoke辅助!wePokE软件透明挂,wepoke系统-一直真的有挂(普及教程)1、不需要AI权...
透明辅助挂!WepokE软件透... 透明辅助挂!WepokE软件透明挂,wpk微扑克辅助是真的-果真真的有挂(必胜教程)1、不需要AI权...
德州辅助!we-poker软件... 德州辅助!we-poker软件透明挂,微扑克有稳赢的打法-的确真的有挂(详细教程);人气非常高,ai...
软件辅助挂!wePoKe软件透... 软件辅助挂!wePoKe软件透明挂,GG扑克辅助软件-的确真的有挂(总结教程)您好,GG扑克,确实是...
软件辅助挂!WepokE软件透... 软件辅助挂!WepokE软件透明挂,红龙扑克模拟器-好像真的有挂(玩家教程)是一款可以让一直输的玩家...
透明辅助!wepokE软件透明... 透明辅助!wepokE软件透明挂,wepoke有插件-一直真的有挂(必胜教程);是一款可以让一直输的...
黑科技辅助挂!WepoKe软件... 黑科技辅助挂!WepoKe软件透明挂,微扑克真的有外挂嘛-一直真的有挂(解密教程)1、超多福利:超高...
脚本辅助挂!wepoker软件... 脚本辅助挂!wepoker软件透明挂,微扑克全自动机器人-果然真的有挂(玩家教你)1、微扑克ai机器...