按优先级合并预约(开始时间和结束时间)
创始人
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为当前预约的开始时间和结束时间。

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

相关内容

热门资讯

黑科技辅助(微扑克代码)线上w... 黑科技辅助(微扑克代码)线上wpk德州起初有挂!太坑了切实有挂(2022已更新)(哔哩哔哩);亲真的...
黑科技教程(wpk助手)德州都... 黑科技教程(wpk助手)德州都是真的有挂!太夸张了从来真的有挂(2025已更新)(哔哩哔哩);德州软...
黑科技线上(德州之星app)A... 黑科技线上(德州之星app)AAPoKER从前真的有挂!太离谱了原生真的有挂(2021已更新)(哔哩...
黑科技最新(wpk插件)WEP... 黑科技最新(wpk插件)WEPoke先前真的有挂!太实锤了原来有挂(2026已更新)(哔哩哔哩)WE...
黑科技中牌率(聚星扑克德州)w... 1、黑科技中牌率(聚星扑克德州)wpk德州往昔是真的有挂!太实锤了素来是有挂(2023已更新)(哔哩...
黑科技最新(WPK必备)wpk... 黑科技最新(WPK必备)wpk游戏一贯真的有挂!太夸张了最初有挂(2020已更新)(哔哩哔哩);玩家...
黑科技实锤(Wepoke苹果)... 黑科技实锤(Wepoke苹果)wpK一贯有挂!太离谱了都是真的是有挂(2021已更新)(哔哩哔哩)是...
黑科技辅助(Wepoke ap... 黑科技辅助(Wepoke app)wpK真是真的是有挂!太嚣张了本然是真的有挂(2026已更新)(哔...
黑科技辅助挂(手机德州app)... 黑科技辅助挂(手机德州app)pokermaster本然存在有挂!太实锤了起初真的有挂(2022已更...
黑科技智能ai(最新版wpk)... 此外,数据分析德州(wpK)辅助神器app还具备辅助透视行为开挂功能,通过对客户wpK透明挂的深入研...