- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有点愚蠢的问题,但很有趣,在我看来,它应该是一个“已解决的问题”。我主要只是对算法感兴趣,我可以自己处理实现。
规范是:
users = [
"Alice",
"Bob",
"Carl",
"Dani",
"Elmer",
]
chores = [
"Kitchen",
"Dining room",
"Upstairs bathroom",
"Living room",
"Lawn",
]
exclusion_list = [
("Bob", "Upstairs bathroom")
]
weekly_offset = 0
# Generate a list of chores "doable" by each user
# Horrible method I know, but just trying to get something working
# and for trivial n and m it shouldn't matter.
user_list = {}
for i in users:
temp_list = []
for j in chores:
for k in exclusion_list:
if k[0] == i and k[1] == j:
print("Excluded")
else:
temp_list.append(j)
user_list[i] = temp_list
# Confirm this list is accurate
print(user_list)
print()
user_offset = 0
for i in user_list:
print(i, end = ": ")
chore_index = (weekly_offset + user_offset) % len(user_list[i])
print(user_list[i][chore_index])
user_offset += 1
最佳答案
The specs are:
Assume a house of n people.
Assume m chores.
For now, for simplicity's sake, assume n == m.
Assume an exclusion list of tuple, ie, Bob doesn't have to ever clean the upstairs bathroom since he lives in a different part of the house, with his own private bathroom. He is however responsible for the other chores.
Assume a "weekly offset" integer variable that is incremented on disk. If this variable is not incremented, the program spits out the same output each time. For now, I'm simply incrementing this variable manually.
No two people should be assigned the same chore for a given week.
Each person should do "each chore they are capable of doing" exactly once before repeating a chore.
exclusion_list
,即第四个条件。
exclusion_list
,一种可能的解决方案如下:
# Abbreviations:
# K == "Kitchen", D == "Dining room", U == "Upstairs bathroom"
# Lr == "Living room", L == "Lawn"
Week 1 2 3 4 5
Alice: K -> D -> U -> Lr -> L
Bob: D -> U -> Lr -> L -> K
Carl: U -> Lr -> L -> K -> D
Dani: Lr -> L -> K -> D -> U
Elmer: L -> K -> D -> U -> Lr
exclusion_list
, Bob 应该有一个长度为 4 的链。这意味着 Bob 将在 5 周内完成 4 项不同的家务活,如下所示:
Week 1 2 3 4 5
Bob: D -> Lr -> L -> K -> D
(5 kinds of chores) * (5 weeks) = 25
要做的家务。并且,由于其他 4 个用户将在 5 周内做 5 种不同类型的家务(因为他们应该有一个长度为 5 的链),分配给 4 个用户后剩余的家务数量为
25 - ((5 kinds of chores) * (4 users)) = 25 - 20 = 5
.这 5 件家务活都是不同的种类。
它与 相矛盾Bob 应该有一个长度为 4 的链。
users = [
"Alice",
"Bob",
]
chores = [
"Kitchen",
"Dining room",
]
exclusion_list = [
("Bob", "Kitchen")
]
关于algorithm - 为家庭创造一个家务轮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61945445/
我是一名优秀的程序员,十分优秀!