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

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

相关内容

热门资讯

开挂辅助!天天互娱app辅助,... 开挂辅助!天天互娱app辅助,拱趴游戏破解器(透视)开挂辅助插件(存在有挂);详细天天互娱app辅助...
开挂辅助!大众互娱脚本,wep... 开挂辅助!大众互娱脚本,wepoker是不是有人用挂(透视)开挂辅助平台(有挂方略);一、大众互娱脚...
开挂辅助!蜀山手游全自动辅助,... 开挂辅助!蜀山手游全自动辅助,wepoker怎么提高运气(透视)开挂辅助脚本(今日头条);亲真的是有...
辅助开挂!龙岩闲游安卓脚本,w... 辅助开挂!龙岩闲游安卓脚本,wepoker轻量版有透视吗(透视)开挂辅助神器(有挂方式);大家肯定在...
开挂辅助!哥哥打大a游戏攻略,... 开挂辅助!哥哥打大a游戏攻略,wepoker私人局有透视吗(透视)开挂辅助插件(果真有挂);哥哥打大...
开挂辅助!h5大厅反杀,德扑H... 【福星临门,好运相随】;开挂辅助!h5大厅反杀,德扑HHpoker有挂吗(透视)开挂辅助神器(有挂教...
开挂辅助!微信小程序微乐游戏辅... 开挂辅助!微信小程序微乐游戏辅助,德普之星透视(透视)开挂辅助神器(有人有挂);微信小程序微乐游戏辅...
辅助开挂!杭州边锋辅助软件,w... 辅助开挂!杭州边锋辅助软件,wpk辅助插件(透视)开挂辅助脚本(发现有挂);是一款可以让一直输的玩家...
开挂辅助!吉安中至小程序微信辅... 开挂辅助!吉安中至小程序微信辅助,wepokerplus万能挂(透视)开挂辅助脚本(有挂教程);相信...
开挂辅助!雀友会广东潮汕麻雀万... 开挂辅助!雀友会广东潮汕麻雀万能辅助器,wepoker免费脚本咨询(透视)开挂辅助下载(有挂秘籍);...