按优先级合并预约(开始时间和结束时间)
创始人
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(的确有挂)-哔哩哔哩暗藏猫腻,小编详细说...
有消息称!拱趴大菠萝辅助器最新... 有消息称!拱趴大菠萝辅助器最新更新内容介绍(辅助)竟然有挂辅助平台(有挂功能)-哔哩哔哩进入游戏-大...
黑科技辅助挂!sohoopok... 黑科技辅助挂!sohoopoker辅助,蜀山四川辅助,机巧辅助教程(竟然有挂)-哔哩哔哩1、黑科技辅...
有消息称!新海贝之城游戏攻略(... 有消息称!新海贝之城游戏攻略(辅助)原来真的是有辅助app(有挂方略)-哔哩哔哩1、下载好新海贝之城...
据目击者称!德州透视插件,科乐... 据目击者称!德州透视插件,科乐填大坑作必弊码,总结辅助插件(有挂存在)-哔哩哔哩1、科乐填大坑作必弊...
为切实保障!老友广东麻将有挂吗... 为切实保障!老友广东麻将有挂吗(辅助)真是有挂辅助下载(新版有挂)-哔哩哔哩1、完成老友广东麻将有挂...
技巧辅助挂!pokerworl... 技巧辅助挂!pokerworld修改器,科乐天天踢有没有作比,要领辅助器(有挂技术)-哔哩哔哩科乐天...
此事备受玩家关注!多乐游戏小程... 此事备受玩家关注!多乐游戏小程序辅助(辅助)一直是有辅助神器(有挂规律)-哔哩哔哩1、下载好多乐游戏...
2026版复盘!werplan... 2026版复盘!werplan外开挂,红龙poker辅助,绝活儿辅助攻略(确实有挂)-哔哩哔哩1.红...