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

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

相关内容

热门资讯

分享认知!hhpoker透视脚... 分享认知!hhpoker透视脚本,德州透视脚本,攻略方法(有挂攻略);玩家必备必赢加哟《136704...
总算清楚!德普之星怎么开辅助,... 总算清楚!德普之星怎么开辅助,wpk刷入池率脚本,2025教程(有挂技巧)科技教程也叫必备教程,这是...
科技通报!pokemmo手机脚... 科技通报!pokemmo手机脚本,newpoker怎么安装脚本,2025新版教程(有挂技巧)这是由厦...
三分钟了解!红龙poker辅助... 三分钟了解!红龙poker辅助器免费观看,wepokerplus脚本,技巧教程(有挂教程);亲真的是...
科技通报!hhpoker德州透... 科技通报!hhpoker德州透视挂,wejoker内置辅助,科技教程(有挂方法);小薇(透视辅助)致...
攻略讲解!wepoker透视底... 攻略讲解!wepoker透视底牌,wepoker免费辅助器,规律教程(有挂技巧)科技教程也叫必备教程...
玩家必看科普!hhpoker透... 1、玩家必看科普!hhpoker透视工具,wepoker代打辅助机器人,玩家教你(有挂透明)。2、w...
科技揭秘!wejoker开挂,... 科技揭秘!wejoker开挂,竞技联盟辅助插件,AI教程(有挂透视)这是由厦门游乐互动科技有限公司精...
盘点一款!拱趴大菠萝开挂方法,... 1、盘点一款!拱趴大菠萝开挂方法,sohoo poker辅助,解说技巧(有挂透视)。2、拱趴大菠萝开...
揭秘几款!aapoker怎么开... 1、揭秘几款!aapoker怎么开辅助器,wejoker透视方法,2025新版总结(有挂透视)。2、...