寻找解决python问题的不同方法

寻找解决python问题的不同方法,python,Python,因此,本周我在编码课上的作业是解决聚会的最佳时间问题(这是解释作业的pdf文件)。我已经找出了问题的代码,但我对练习2感到困惑,练习2要求我找到一种不依赖于时间粒度的替代方法。我不知道如何解决这个问题,我能得到一些帮助吗?谢谢大家! 我的原始问题代码如下 sched = [(6, 8), (6, 12), (6, 7), (7, 8), (7, 10), (8, 9), (8, 10), (9, 12),(9, 10), (10, 11), (10, 12), (11, 12)] sched2

因此,本周我在编码课上的作业是解决聚会的最佳时间问题(这是解释作业的pdf文件)。我已经找出了问题的代码,但我对练习2感到困惑,练习2要求我找到一种不依赖于时间粒度的替代方法。我不知道如何解决这个问题,我能得到一些帮助吗?谢谢大家!

我的原始问题代码如下

sched = [(6, 8), (6, 12), (6, 7), (7, 8), (7, 10), (8, 9), (8, 10), (9, 12),(9, 10), (10, 11), (10, 12), (11, 12)]
sched2 = [(6.0, 8.0), (6.5, 12.0), (6.5, 7.0), (7.0, 8.0), (7.5, 10.0), (8.0, 9.0),(8.0, 10.0), (9.0, 12.0), (9.5, 10.0), (10.0, 11.0), (10.0, 12.0), (11.0, 12.0)]


def bestTimeToPartySmart(schedule, start, end):

    times = []
    for c in schedule:
        times.append((c[0], 'start'))
        times.append((c[1], 'end'))


    sortlist(times)

    maxcount, time = chooseTimeConstrained(times, start, end)

    print ('Best time to attend the party is at', time,\
           'o\'clock', ':', maxcount, 'celebrities will be attending!')
    


def sortlist(tlist):
    for index in range(len(tlist)-1):
        ismall = index
        for i in range(index, len(tlist)):
            if tlist[ismall][0] > tlist[i][0] or \
               (tlist[ismall][0] == tlist[i][0] and \
                tlist[ismall][1] > tlist[i][1]):
                ismall = i
        tlist[index], tlist[ismall] = tlist[ismall], tlist[index]
    
    return


def chooseTimeConstrained(times, start, end):

    rcount = 0
    maxcount = 0
    time = 0
    
    for t in times:
        if t[1] == 'start':
            rcount = rcount + 1
        elif t[1] == 'end':
            rcount = rcount - 1
        if rcount > maxcount and t[0] >= start and t[0] < end:
            maxcount = rcount
            time = t[0]

    return maxcount, time


bestTimeToPartySmart(sched2, 10.0, 12.0)
    
sched=[(6,8)、(6,12)、(6,7)、(7,8)、(7,10)、(8,9)、(8,10)、(9,12)、(9,10)、(10,11)、(10,12)、(11,12)]
sched2=[(6.0,8.0),(6.5,12.0),(6.5,7.0),(7.0,8.0),(7.5,10.0),(8.0,9.0),(8.0,10.0),(9.0,12.0),(9.5,10.0),(10.0,12.0),(11.0,12.0)]
def BESTTIMETOPARTYMART(计划、开始、结束):
时间=[]
对于附表中的c:
times.append((c[0],'start'))
times.append((c[1],'end'))
分类表(次)
maxcount,time=ChooseTimeConstrated(时间,开始,结束)
打印('参加聚会的最佳时间是',时间\
‘点钟’、’:’、maxcount,‘名人将出席!’
def分类列表(分类列表):
对于范围内的索引(len(tlist)-1):
ismall=索引
对于范围内的i(索引,len(tlist)):
如果tlist[ismall][0]>tlist[i][0]或\
(t列表[ismall][0]==t列表[i][0]和\
tlist[ismall][1]>tlist[i][1]):
ismall=i
tlist[index],tlist[ismall]=tlist[ismall],tlist[index]
返回
def ChooseTimeConstrated(时间、开始、结束):
rcount=0
最大计数=0
时间=0
对于t,时间为:
如果t[1]=“开始”:
rcount=rcount+1
elif t[1]=“结束”:
rcount=rcount-1
如果rcount>maxcount且t[0]>=开始且t[0]<结束:
maxcount=rcount
时间=t[0]
返回最大计数,时间
bestTimeToPartySmart(附表2,10.0,12.0)

基于pdf,我们的想法是检查每一位名人的到来,并确定当名人到来时有多少其他名人已经在聚会上。当你找到最大计数时,你应该和那个名人一起到达

下面是一些需要澄清的伪代码:

max = 0
beststarttime = None

For each celeb:  # check each celeb arrival time
   cnt=0
   for each otherceleb:  # check other celebrities already at the party
       if celeb.starttime in (otherceleb.starttime....otherceleb.endtime):
           cnt+=1
   if cnt > max: 
       max = cnt
       beststarttime = celeb.starttime  # you arrive with this celebrity

我会在下午6点到达那里,因为碧昂丝就是在那个时候到达的