按优先级合并预约(开始时间和结束时间)
创始人
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作弊辅助,wpk辅助哪里买,系统教程(切实真的有挂);小薇(透视辅助)致您一...
透视挂透视!hhpoker万能... 透视挂透视!hhpoker万能辅助器,wepoker透视脚本,新手必备(有挂辅助挂)1、下载好辅助软...
透视真的!hhpoker辅助软... 透视真的!hhpoker辅助软件下载,sohoo poker辅助器,玩家教你(总是是有挂)1、玩家可...
透视脚本!wepoker免费透... 透视脚本!wepoker免费透视脚本,wpk辅助插件,推荐十款(有挂教程);1、不需要AI权限,帮助...
透视挂!德普之星透视辅助ios... 透视挂!德普之星透视辅助ios,wepoker辅助器软件下载,大神讲解(都是存在有挂)1、完成德普之...
透视能赢!hhpoker透视方... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去Q群【1067239143】很多玩家在这款游戏中...
透视有挂!wpk辅助软件多少钱... 您好,wpk辅助软件多少钱一个这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很...
透视了解!hhpoker开辅助... 透视了解!hhpoker开辅助软件,hhpoker万能辅助器,科技揭秘(有挂黑科技)1、玩家可以在软...
透视挂!hhpoker透视下载... 透视挂!hhpoker透视下载链接在哪里,wepoker有机器人吗,辅助教程(原本是有挂)1、完成h...
透视好牌!wpk辅助购买,hh... 透视好牌!wpk辅助购买,hhpoker透视功能如何下载,研究成果(有挂教学);1、透视辅助简单,软...