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

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

相关内容

热门资讯

教你透视!wpk透视工作室,w... 教你透视!wpk透视工作室,wpk系统是否存在作比弊行为,一贯一直都是有辅助app(哔哩哔哩)1、辅...
总结透视!wepoker游戏的... 总结透视!wepoker游戏的安装教程,wepoker有辅助器吗,原来是有辅助技巧(哔哩哔哩)1、玩...
科普透视!aapoker透视脚... 科普透视!aapoker透视脚本入口,aapoker破解侠是真的吗,本来存在有辅助神器(哔哩哔哩)1...
科普透视!hhpoker免费透... 科普透视!hhpoker免费透视脚本,德州hhpoker是真的吗,都是存在有辅助攻略(哔哩哔哩)1、...
了解透视!wpk俱乐部怎么作比... 了解透视!wpk俱乐部怎么作比弊,wpk是真的还是假的,一直真的是有辅助攻略(哔哩哔哩)1、玩家可以...
专业透视!aapoker辅助器... 专业透视!aapoker辅助器怎么用,aapoker辅助插件工具,其实是有辅助神器(哔哩哔哩)1、辅...
透视透视!wepoker私人局... 透视透视!wepoker私人局透视插件,wepoker辅助器下载,总是真的是有辅助工具(哔哩哔哩)1...
详细透视!wepoker有辅助... 详细透视!wepoker有辅助插件吗,wepoker辅助是真的吗,果然真的有辅助插件(哔哩哔哩)在进...
有挂透视!wepoker的辅助... 有挂透视!wepoker的辅助器,wepoker透视最简单三个步骤,总是有辅助神器(哔哩哔哩)1、e...
必备透视!hhpoker是内部... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家在这款游戏中打...