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

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

相关内容

热门资讯

透视了解!pokemmo脚本辅... 透视了解!pokemmo脚本辅助器下载,wepoker轻量版辅助,秘籍教程(都是存在有挂)-哔哩哔哩...
透视了解!wepoker免费透... 透视了解!wepoker免费透视脚本,德普之星辅助器app,方式教程(切实是真的挂)-哔哩哔哩暗藏猫...
透视规律!拱趴大菠萝机器人,x... 透视规律!拱趴大菠萝机器人,xpoker怎么辅助,技法教程(好像存在有挂)-哔哩哔哩1、拱趴大菠萝机...
透视了解!wepoker透视功... 透视了解!wepoker透视功能下载,淘宝买wepoker透视有用吗,办法教程(切实真的是有挂)-哔...
透视黑科技!德普之星怎么设置埋... 透视黑科技!德普之星怎么设置埋牌,we-poker靠谱吗,模板教程(竟然真的有挂)-哔哩哔哩1、德普...
透视挂透视!epoker底牌透... 透视挂透视!epoker底牌透视,aapoker怎么提高中牌率,方案教程(其实真的是有挂)-哔哩哔哩...
透视好友!we-poker辅助... 透视好友!we-poker辅助器,wpk辅助哪里买,讲义教程(本来是真的挂)-哔哩哔哩1、起透看视 ...
透视好友房!德普之星的辅助工具... 透视好友房!德普之星的辅助工具介绍,wepoker辅助分析器,大纲教程(果然有挂)-哔哩哔哩1、德普...
透视透视!wepoker手机插... 透视透视!wepoker手机插件,wepoker辅助是真的吗,指引教程(原来真的是有挂)-哔哩哔哩1...
透视智能ai!we poker... 透视智能ai!we poker辅助器,wepoker怎么看底牌,举措教程(本来真的是有挂)-哔哩哔哩...