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

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

相关内容

热门资讯

透视安卓版“贰柒拾智能辅助柒”... 透视安卓版“贰柒拾智能辅助柒”本来有开挂辅助工具(有挂技巧);无需打开直接搜索微信(13670430...
总算了解“中至赣牌圈挂件价格明... 大家好,今天小编来为大家解答中至赣牌圈挂件价格明细这个问题咨询软件客服可以免费测试直接加微信(136...
透视游戏“广东闲来辅助神器免费... 透视游戏“广东闲来辅助神器免费版”先前有辅助开挂挂(有挂技巧);无需打开直接搜索薇:13670430...
一分钟教会你“上饶中至辅助插件... 一分钟教会你“上饶中至辅助插件透视”原先有辅助开挂工具(有挂存在);无需打开直接搜索加薇136704...
透视透视挂“潮汕馆插件”从前有... 【亲,潮汕馆插件 这款游戏可以开挂的,确实是有挂的,很多玩家在这款潮汕馆插件中打牌都会发现很多用户的...
科技分享“樱花之盛能不能开挂”... 科技分享“樱花之盛能不能开挂”从来有辅助开挂挂(有挂存在)1、下载安装好樱花之盛能不能开挂,进入游戏...
玩家必看科普“上饶中至脚本修改... 玩家必看科普“上饶中至脚本修改”最初有辅助开挂神器(有挂方法)您好:上饶中至脚本修改这款游戏可以开挂...
带你了解“小闲54辅助”原本有... 带你了解“小闲54辅助”原本有开挂辅助下载(确实有挂);打开点击测试直接进入微信(136704302...
玩家攻略“潮汕掌上娱透视”先前... 玩家攻略“潮汕掌上娱透视”先前有开挂辅助脚本(讲解有挂);无需打开直接搜索加(薇:136704302...
重大科普“wepoker到底有... wepoker到底有没有透视是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用...