会议日程安排问题python

#File Name : 会议室日程安排问题.py

class Program(object):
    def __init__(self,start,end):
        self.start = start
        self.end = end

def bestArrange(programs,start_time):
    """programs 为项目列表
       strat_time 为最早开始的时间"""
    programs = sorted(programs,key=lambda x:x.end)
    for i in range(len(programs)):
        print(programs[i].start)

    res = 0
    for i in range(len(programs)):
        if start_time<=programs[i].start:
            res+=1
            start_time = programs[i].end
    return  res

if __name__ =='__main__':
    a1 = Program(6, 8)
    a2 = Program(7, 9)
    a3 = Program(6.5, 8.5)
    a4 = Program(9.5, 11)
    a5 = Program(10, 10.5)
    a6 = Program(10.4, 12)
    programs = [a1,a2,a3,a4,a5,a6]
    a = bestArrange(programs,6)
    print(a)

sorted 用法

>>>a = [5,7,6,3,4,1,2]
>>> b = sorted(a)       # 保留原列表
>>> a 
[5, 7, 6, 3, 4, 1, 2]
>>> b
[1, 2, 3, 4, 5, 6, 7]
 
>>> L=[('b',2),('a',1),('c',3),('d',4)]
>>> sorted(L, cmp=lambda x,y:cmp(x[1],y[1]))   # 利用cmp函数
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
>>> sorted(L, key=lambda x:x[1])               # 利用key
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
 
 
>>> students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
>>> sorted(students, key=lambda s: s[2])            # 按年龄排序
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
 
>>> sorted(students, key=lambda s: s[2], reverse=True)       # 按降序
[('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
>>>

 

你可能感兴趣的:(算法牛人之路)