我有一個專案,我想用一組規則制作一個串列隨機化器。
假設我有 1 個有 9 個專案的命名客人串列,1 個有 4 個專案的命名主機,以及一個有 3 個專案的命名餐點。
現在我想將這些客人隨機分配到主機中,這樣每個主機都有 3 位客人,巫婆就夠公平了。現在它們都將連接到膳食中的第一項。當我想讓他們在不同的主人之間再次爭奪第 2 餐時,問題就出現了,但是這樣一來,已經在一起的客人都不會在接下來的 2 餐中再次見面了。
我最終確實想要擁有它,這樣我就可以擁有一個動態的主人和客人串列,這些串列在輸入后會自行解決。
我不想解決代碼,只是尋找到達那里的方法。
uj5u.com熱心網友回復:
您需要跟蹤兩件事:a) 哪些主人已經接待了哪些客人 b) 對于每頓飯,已經分配了哪些客人。
專案 a 可以通過使用每個主機的名稱作為鍵的字典來完成。專案 b 只需要是一個串列,為每頓新餐重置。
一旦您開始跟蹤該資訊,然后就在選擇將分配給房東的客人之前,您可以使用您正在跟蹤的這些資訊來創建第二個中介串列,其中僅包含可以選擇的客人并隨機選擇其中之一。
下面是我對這個問題的解決方案:
import random
class Meal_Schedule:
''' A class for repeatedly dividing up multiple hosts amongst multiple guests
such that a guest will never be hosted by the same host twice
and will always be placed with different guests '''
def __init__(self, guests: list, hosts: list, meals: list):
''' guests and hosts should both be lists. Duplicate guest or host names are not supported,
and the duplicate names will be ignored. '''
self.guests = guests
self.hosts = hosts
self.meals = meals
self.previously_assigned = {host:[] for host in hosts}
def create_meal_schedule(self):
''' Do all of the scheduling
Example of the dictionary returned:
{
'Hamburger': {'host 1': ['Guest D', 'Guest A'],
'host 2': ['Guest B', 'Guest C']},
'Pizza': {'host 1': ['Guest B', 'Guest C'],
'host 2': ['Guest A', 'Guest D']}
}
'''
ret = {}
for meal in self.meals:
ret[meal] = self.plan_next_meal()
return ret
def plan_next_meal(self, guests_per_host=None):
''' Plans the next meal
guests_per_host should be an integer greater than 1, or None to automatically calculate a sensible value
Returns a dictionary containing each hosts guest list for the next meal '''
# self.previously_assigned keeps track of who has been assigned to a host in the past
# so we don't assign a guest to the same host twice
# and assigned_this_round will keep track of which guests have already been given a seat this round
# so we don't double book a guest to two different hosts
if guests_per_host is None:
if not self.hosts:
# So we don't get a vague division by zero error, we raise this error instead
raise RuntimeException('Must have at least one host')
guests_per_host = len(self.guests) // len(self.hosts)
if guests_per_host < 1:
guests_per_host = 1
assert guests_per_host > 1, 'guests_per_host must be an integer greater than 1'
ret = {}
assigned_this_round = []
for host in self.hosts:
guest_list = _create_guest_list
guest_list = _create_guest_list( host, guests_per_host,
exclude_these_guests = [*self.previously_assigned[host], *assigned_this_round] )
assigned_this_round.extend(guest_list)
ret[host] = guest_list
self.previously_assigned[host].extend(guest_list)
return ret
def _create_guest_list(host, guests_per_host, exclude_these_guests):
''' used when planning a meal to create a host's guest list '''
guests_pool = [ guest for guest in self.guests if guest not in exclude_these_guests ]
if len(guests_pool) <= guests_per_host:
return guests_pool
return random.sample(guests_pool, guests_per_host)
def display_schedule(self, schedule=None):
''' displays a schedule created with create_meal_schedule
on the console in a nice way '''
if schedule is None:
schedule = self.create_meal_schedule()
print()
for meal, plan in schedule.items():
print(f'-- {meal} Meal Plan --'.upper())
print()
for host, guest_list in plan.items():
print(f'Host: {host}')
print('Guess list:')
for guest in guest_list:
print(f' {guest}')
print()
print()
print()
guests = [f'Guest {x}' for x in 'ABCDEFGHI']
hosts = ['host1', 'host2', 'host3', 'host4']
meals = ['Hamburger', 'Pizza', 'Eggs']
scheduler = Meal_Schedule(guests, hosts, meals)
scheduler.display_schedule()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/367976.html
