Programming for the Puzzled

Fun!🤗

```
# Q1
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 bestTimeToPartyStart(schedule, participationTime):
times = []
for c in schedule:
# search celeblities who are at the time I am participating
if c[1] > participationTime[0] and participationTime[1] > c[0]:
times.append((c[0], 'start'))
times.append((c[1], 'end'))
sortList(times)
maxCount, time = chooseTime(times)
print('Best time to attend the party is at', time, "o'clock", ':', maxCount, 'celebrities will be attending!')
def sortList(times):
for p in range(len(times) - 1):
right = p
for s in range(p, len(times)):
if times[right][0] > times[s][0]:
right = s
if p != right:
times[p], times[right] = times[right], times[p]
def chooseTime(times):
maxCount = 0
time = 0
count = 0
for t in times:
if t[1] == 'start':
count += 1
elif t[1] == 'end':
count -= 1
if count > maxCount:
maxCount = count
time = t[0]
return maxCount, time
# run
bestTimeToPartyStart(sched2, (10.0, 12.0))
```

```
#Q2
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 bestTimeToPartyStart(schedule):
maxCount, celeb = celeblityAreManyTogether(schedule)
print('Best time to attend the party is at', schedule[celeb][0], "o'clock", ':', maxCount, 'celebrities will be attending!')
def celeblityAreManyTogether(schedule):
maxCount = 0
manyTogether= 0
for i in range(len(schedule)):
count = 0
for j in range(len(schedule)):
# is start time at the time who is participating
if schedule[i][0] <= schedule[j][0] and schedule[i][1] > schedule[j][0]:
count += 1
if maxCount < count:
maxCount = count
manyTogether = i
print(manyTogether )
return maxCount, manyTogether
# run
bestTimeToPartyStart(sched2)
```

```
#Q3
sched2 = [(6.0, 8.0, 2), (6.5, 12.0, 1), (6.5, 7.0, 2), (7.0, 8.0, 2),
(7.5, 10.0, 3), (8.0, 9.0, 2), (8.0, 10.0, 1), (9.0, 12.0, 2),
(9.5, 10.0, 4), (10.0, 11.0, 2), (10.0, 12.0, 3), (11.0, 12.0, 7)]
def bestTimeToPartyStart(schedule):
times = []
for c in schedule:
times.append((c[0], 'start', c[2]))
times.append((c[1], 'end', c[2]))
sortList(times)
maxPriority, time = chooseTime(times)
print('Best time to attend the party is at ', time, " o'clock", ': priority =', maxPriority)
def sortList(times):
for p in range(len(times) - 1):
right = p
for s in range(p, len(times)):
if times[right][0] > times[s][0]:
right = s
if p != right:
times[p], times[right] = times[right], times[p]
def chooseTime(times):
maxPriority = 0
time = 0
priority = 0
for t in times:
if t[1] == 'start':
# use priority value instead of time
priority += t[2]
elif t[1] == 'end':
priority -= t[2]
if priority > maxPriority:
maxPriority = priority
time = t[0]
return maxPriority, time
# run
bestTimeToPartyStart(sched2)
```

## Top comments (0)