游客统计问题

# 派对从中午十二点到晚上八点,客人到来和离开会签到,请统计哪个小时段内在场客人最多
# 每半小时分为一个区间,统计哪个时间短的客人最多

  1 import random, datetime
  2 
  3 
  4 def product_guest():
  5     '''
  6     num_guest: 随机产生顾客是数量
  7     :return: 随机产生顾客是数量
  8     '''
  9     num_guest = random.randint(0, 100)
 10     return num_guest
 11 
 12 
 13 def product_time(start_time, end_time, num_guest):
 14     start_time_args_list = start_time.split(':')
 15     end_time_args_list = end_time.split(':')
 16     num_guest = num_guest
 17 
 18     come_hours_list = []
 19     leave_hours_list = []
 20 
 21     for i in range(num_guest):
 22         come_hours = random.randint(int(start_time_args_list[0]), int(end_time_args_list[0]) - 1)
 23         come_hours_list.append(str(come_hours))
 24         leave_hours_list.append(str(random.randint(come_hours, int(end_time_args_list[0]) - 1)))
 25 
 26     come_min_list = []
 27     leave_min_list = []
 28 
 29     for i in range(num_guest):
 30         come_min = random.randint(0, 59)
 31         if come_min < 10:
 32             come_min_list.append('0' + str(come_min))
 33         else:
 34             come_min_list.append(str(come_min))
 35 
 36         leave_min = random.randint(come_min, 59)
 37         if leave_min < 10:
 38             leave_min_list.append('0' + str(random.randint(come_min, 59)))
 39         else:
 40             leave_min_list.append(str(random.randint(come_min, 59)))
 41 
 42     come_sec_list = []
 43     leave_sec_list = []
 44 
 45     for i in range(num_guest):
 46         come_sec = random.randint(0, 59)
 47         if come_sec < 10:
 48             come_sec_list.append('0' + str(come_sec))
 49         else:
 50             come_sec_list.append(str(come_sec))
 51 
 52         if come_hours_list[i] == leave_hours_list[i] and come_min_list[i] == leave_min_list[i]:
 53             leave_sec = random.randint(come_sec, 59)
 54         else:
 55             leave_sec = random.randint(0, 59)
 56 
 57         if leave_sec < 10:
 58             leave_sec_list.append('0' + str(leave_sec))
 59         else:
 60             leave_sec_list.append(str(leave_sec))
 61 
 62     time_list = [[come_hours_list, leave_hours_list], [come_min_list, leave_min_list], [come_sec_list, leave_sec_list]]
 63     return time_list
 64 
 65 
 66 def guest_time(time_list, num_guest):
 67     guest_message_dict = {}
 68     for i in range(num_guest):
 69         guest_message_dict[f'客人{i}'] = {
 70             'come_time': ':'.join((time_list[0][0][i], time_list[1][0][i], time_list[2][0][i])),
 71             'leave_time': ':'.join((time_list[0][1][i], time_list[0][1][i], time_list[2][1][i]))}
 72     return guest_message_dict
 73 
 74 
 75 def max_guest_time(guest_message_dict, start_time, end_time):
 76     start_q = int(start_time.strip(':')[0])
 77     time_q_dict = {0: '12:00:00', 1: '12:30:00', 2: '13:00:00', 3: '13:30:00', 4: '14:00:00', 5: '14:30:00',
 78                    6: '15:00:00', 7: '15:30:00', 8: '16:00:00', 9: '16:30:00', 10: '17:00:00', 11: '17:30:00'}
 79 
 80     dict_time_num = {}
 81     for i in range((int(end_time.split(':')[0]) - int(start_time.split(':')[0])) * 2):
 82         dict_time_num[i] = 0
 83 
 84     guest_name = list(guest_message_dict.keys())
 85     for i in guest_name:
 86         guest_come_hour_min = guest_message_dict[i]['come_time'].split(':')
 87         guest_leave_hour_min = guest_message_dict[i]['leave_time'].split(':')
 88         if int(guest_come_hour_min[1]) < 30:
 89             time_num_come = (int(guest_come_hour_min[0]) - 12) * 2
 90         else:
 91             time_num_come = (int(guest_come_hour_min[0]) - 12) * 2 + 1
 92 
 93         if int(guest_come_hour_min[1]) < 30:
 94             time_num_leave = (int(guest_leave_hour_min[0]) - 12) * 2
 95         else:
 96             time_num_leave = (int(guest_leave_hour_min[0]) - 12) * 2 + 1
 97 
 98         for i in range(time_num_come, time_num_leave + 1):
 99             dict_time_num[i] += 1
100 
101     max_num = max(list(dict_time_num.values()))
102 
103     result = {}
104 
105     for i in dict_time_num.keys():
106         if dict_time_num[i] == max_num:
107             result[i] = {'时间': time_q_dict[i], 'count': dict_time_num[i]}
108 
109     return result
110 
111 
112 if __name__ == '__main__':
113     start_time = '12:00:00'
114     end_time = '18:00:00'
115 
116     num_guest = product_guest()
117     time_list = product_time(start_time, end_time, num_guest)
118 
119     guest_message_dict = guest_time(time_list, num_guest)
120 
121     print(max_guest_time(guest_message_dict, start_time, end_time))

 

posted @ 2020-10-14 15:26  AKA绒滑服贵  阅读(100)  评论(0编辑  收藏  举报