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

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

相关内容

热门资讯

透视科普!wepoker游戏的... 透视科普!wepoker游戏的安装教程,wepoker游戏安装教程-确实有辅助脚本(哔哩哔哩)1、任...
透视详情!wepoker有辅助... 透视详情!wepoker有辅助工具吗,wepoker辅助是真的吗-切实是有辅助app(哔哩哔哩)1)...
透视有挂!wepoker怎么设... 透视有挂!wepoker怎么设置盖牌,wepoker透视最简单三个步骤-总是真的是有辅助插件(哔哩哔...
透视揭幕!wpk官网下载链接,... 透视揭幕!wpk官网下载链接,wpk真的有透视嘛-果然真的有辅助攻略(哔哩哔哩)1、任何透视是真的假...
透视辅助!wpk插件辅助,wp... 透视辅助!wpk插件辅助,wpk控制牌是真的吗-其实是有辅助神器(哔哩哔哩)1、起透看视 辅助软件价...
透视专业!wpk透视辅助下载,... 透视专业!wpk透视辅助下载,wpk有作弊吗-一直一直总是有辅助app(哔哩哔哩)1、脚本辅助下载、...
透视曝光!德普之星辅助软件,德... 透视曝光!德普之星辅助软件,德普之星透视辅助软件激活码-一直有辅助插件(哔哩哔哩)该软件可以轻松地帮...
透视详情!aapoker万能辅... 透视详情!aapoker万能辅助器,aa poker透视软件-真是有辅助技巧(哔哩哔哩)1、下载好透...
透视普及!德普之星透视辅助,德... 透视普及!德普之星透视辅助,德普之星怎么设置埋牌-总是真的是有辅助攻略(哔哩哔哩)在进入软件靠谱后,...
透视辅助!德州局怎么透视,hh... 透视辅助!德州局怎么透视,hhpoker辅助靠谱吗-总是存在有辅助教程(哔哩哔哩)1、金币登录送、破...