The best time to party
Solution
""" solution.py """
# The best time to party
# -- frequency distribution with dictionary
schedule = {'Beyoncé': (6, 7),
'Taylor': (7, 9),
'Brad': (10, 11),
'Katy': (10, 12),
'Tom': (8, 10),
'Drake': (9, 11),
'Alicia': (6, 8)}
tally = {}
for _, (t1, t2) in schedule.items():
for t in range(t1, t2):
freq = tally.get(t, 0)
freq += 1
tally[t] = freq
print(tally)
# -- frequency distribution with list (array)
tally = 12 * [0]
for _, (t1, t2) in schedule.items():
for t in range(t1, t2):
tally[t - 1] += 1
print(tally)
# -- count of celebrities at arrival times
hour = {t for _, (t, _) in schedule.items()}
tally = {t: 0 for t in hour}
for t in tally:
for _, (t1, t2) in schedule.items():
if t1 <= t < t2:
tally[t] += 1
print(tally)
# -- weighted celebrities
schedule = [(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)]
time = {t for t, _, _ in schedule}
weight = {t: 0 for t in time}
for t in weight:
for t1, t2, w in schedule:
if t1 <= t < t2:
weight[t] += w
print(weight)Output
{6: 2, 7: 2, 8: 2, 10: 3, 11: 1, 9: 2}
[0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 1, 0]
{6: 2, 7: 2, 8: 2, 9: 2, 10: 3}
{6.0: 2, 7.0: 5, 8.0: 7, 7.5: 8, 6.5: 5, 9.0: 7, 9.5: 11, 10.0: 8, 11.0: 13}