gpt4 book ai didi

python - python 中用于实现 37% 规则的字典

转载 作者:行者123 更新时间:2023-11-30 22:11:26 24 4
gpt4 key购买 nike

我正在尝试布莱恩·克里斯蒂安 (Brian Christian) 所著的《生活算法》一书中著名的 37% 规则。

37% 规则基本上是说,当您需要在有限的时间内筛选一系列选项时 - 无论是工作候选人、新公寓还是潜在的浪漫伴侣 - 做出决定的最佳时机是您已经看过其中 37% 的选项。

在选择过程中,您将收集足够的信息来做出明智的决定,但您不会浪费太多时间寻找不必要的选项。在 37% 的分数上,您处于一个很好的位置,可以从中挑选出最好的。

证明这一理论的常见思想实验是由非电脑数学专家在 20 世纪 60 年代开发的,称为“秘书问题”。

该计划正在运行,但我想开始考虑在 37% 的候选人之后选择候选人。由于我使用的是字典,因此我无法访问指定数量的候选者之后的元素。我怎样才能做到这一点?

import matplotlib.pyplot as plt
# for visualising scores
def initiate(candidates):
print("Total candidates are : ",len(candidates))
lookup_num=int(len(candidates) *0.37)
#finds 37% of the candidates
average=lookup(candidates)
# returns average of lookup phase
chosen=select_cad(candidates,lookup_num,average)
# selects a candidate based on lookUp average
print("The chosen candidate is : {} ".format(chosen))

def lookup(candidates):
average_score=0
for cands,score in candidates.items():
average_score+=score
average_score=int(average_score/len(candidates))
print("The average score in lookup is : ",average_score)
#return the average score to average local variable
return average_score



def select_cad(candidates,lookup_num,average):
for cands,score in candidates.items():
if(score>average):
return cands
else:
continue
print("Something went wrong!")
quit


candidates={"Husain":85, "Chirag":94 ,"Asim":70,"Ankit":65 ,"Saiteja":65 ,"Absar":75 ,"Premraj":70 ,"Sagar":75 ,"Himani":75 ,"Parth":76 ,"Sumedha":70 ,"Revati":65 ,"Sageer":65 ,"Noorjahan":60 ,"Muzammil":65 ,"Shifa":56 , "Dipti":65 , "Dheeraj":70 }
initiate(candidates)

plt.bar(range(len(candidates)), list(candidates.values()), align='center', color='green')
plt.xticks(range(len(candidates)), list(candidates.keys()))
plt.show()

即使在选择阶段,如何才能更灵活地更新平均分数?

最佳答案

刚刚阅读了这个“37% 规则”,所以我希望我理解正确。我会实现类似的东西:

import random
def rule_of_37(candidates):
# first I date random 37% of the candidates
who_i_date = random.sample(list(candidates), int(len(candidates)*.37))
print("I meet those 37% of the candidates", who_i_date)
# then I calculate their average score
average = sum(candidates[name] for name in who_i_date) / len(who_i_date)
print("The average score was", average)
# then I settle with the next person that has a score higher than the average (obviously I cannot re-date candidates)
# hopefully there is still someone with an higher score than average...
try:
who_i_marry = next(name
for name, score in candidates.items()
if name not in who_i_date
and score > average)
print("I end up with", who_i_marry, "who has a score of", candidates[who_i_marry])
except StopIteration:
print("I end up all alone, there was nobody left with an higher score than", average, "...")

candidates={"Husain":85, "Chirag":94 ,"Asim":70,"Ankit":65 ,"Saiteja":65 ,"Absar":75 ,"Premraj":70 ,"Sagar":75 ,"Himani":75 ,"Parth":76 ,"Sumedha":70 ,"Revati":65 ,"Sageer":65 ,"Noorjahan":60 ,"Muzammil":65 ,"Shifa":56 , "Dipti":65 , "Dheeraj":70 }

rule_of_37(candidates)

示例执行(您的示例可能会有所不同,因为前 37% 的候选者是随机挑选的):

I meet those 37% of the candidates ['Dipti', 'Chirag', 'Revati', 'Sumedha', 'Dhe eraj', 'Muzammil']

The average score was 71.5

I end up with Husain who has a score of 85

如果您想自己选择第一批候选人而不是依靠随机,您可以简单地将 who_i_date 替换为您预先选择的列表:

who_i_date = ['Husain', 'Chirag', 'Asim', 'Ankit', 'Saiteja', 'Absar']

但是其他 63% 将被任意排序,因此您可能不会总是选择相同的(除非您使用默认情况下保持字典顺序的 Python 3.6+)。如果您想按顺序确定剩余 63% 的日期,则必须迭代候选人姓名列表,而不是遍历将姓名映射到分数的字典。我把这个留给你。

关于python - python 中用于实现 37% 规则的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51405822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com