按优先级合并预约(开始时间和结束时间)
创始人
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),太坑了原来是真的有挂(有挂攻略)-哔哩哔哩;致您一封信...
6分钟了解(微扑克机器人)外挂... 6分钟了解(微扑克机器人)外挂透明挂辅助神器(ai代打)透视辅助(2024已更新)(哔哩哔哩);微扑...
科技分享(德扑之星开桌)外挂透... 科技分享(德扑之星开桌)外挂透明挂辅助工具(软件)德州ai机器人(2024已更新)(哔哩哔哩)是一款...
推荐攻略!边锋杭麻圈有挂(We... 推荐攻略!边锋杭麻圈有挂(WepokE),太坑了的确真的有挂(有挂教程)-哔哩哔哩;亲真的是有正版授...
四分钟了解!边锋老友棋牌一直输... 四分钟了解!边锋老友棋牌一直输,来玩德州 辅助,详细有挂(有挂攻略)-哔哩哔哩;边锋老友棋牌一直输软...
最新技巧!雀神小程序游戏规则(... 最新技巧!雀神小程序游戏规则(辅助挂)太坑了竟然是真的有挂(有挂教程)-哔哩哔哩;雀神小程序游戏规则...
七分钟了解(wePOke)外挂... 七分钟了解(wePOke)外挂透明挂辅助机制(系统)软件透明挂(2023已更新)(哔哩哔哩);(需添...
重大通报!鄱阳翻精系统发牌机制... 重大通报!鄱阳翻精系统发牌机制(wepoKE),太坑了其实真的有挂(有挂了解)-哔哩哔哩科技教程也叫...
我来教教你!边锋杭麻圈辅助,a... 我来教教你!边锋杭麻圈辅助,appoker透视辅助下载,详细有挂(有挂总结)-哔哩哔哩;边锋杭麻圈辅...
新手必备!广东雀神辅助插件哪里... 新手必备!广东雀神辅助插件哪里买(辅助挂)太坑了确实是真的有挂(有挂教学)-哔哩哔哩;科技详细教程小...