作为一个业余 Python 用户,我使用 Python 代码来模拟经典的科技公司面试问题:
You go to Heaven and see 3 gates.
One leads straight to Heaven.
One sends you to Hell for 1 day, then sends you back to the gates.
One sends you to Hell for 2 days, then sends you back to the gates.
Every time you return to the gates, they will have been shuffled, so you can't tell from past experience which is which. They could even be standing in the same place.
On average, how long would you expect to be tortured by hellfire before you meet God?
我写了一个 Python 代码来模拟这个:
trials = [0]
totalDays = [0]
days = [0]
import random
def chooseGates():
choice = random.randint(1,3)
if choice == 1:
days[0] = 1
print("You chose door 1 and spent 1 day in Hell. You return to the gates, which have been shuffled, and choose again.")
chooseGates()
elif choice == 2:
days[0] = 2
print("You chose door 2 and spent 2 days in Hell. You return to the gates, which have been shuffled, and choose again.")
chooseGates()
else:
print("You chose door 3, which is the right door. Well done, thou good and faithful servant - enter thou into the joy of our Lord.")
trials[0] = 1
totalDays[0] = days[0]
print("You've done ", trials[0], "trials and this time you've spent", days[0], "days in Hell. ")
days[0] = 0
print(" ")
print("The game starts again.")
return True
for i in range (0,10):
chooseGates()
print("A total of " , trials[0], "trials run. ")
print("The average number of days needed was ", totalDays[0]/trials[0])
问题是,这是Programmiz Python Online Compiler 给我的:
You chose door 3, which is the right door. Well done, thou good and faithful servant - enter thou into the joy of our Lord.
You've done 1 trials and this time you've spent 0 days in Hell.
The game starts again.
You chose door 2 and spent 2 days in Hell. You return to the gates, which have been shuffled, and choose again.
You chose door 3, which is the right door. Well done, thou good and faithful servant - enter thou into the joy of our Lord.
You've done 2 trials and this time you've spent 2 days in Hell.
The game starts again.
You've done 3 trials and this time you've spent 0 days in Hell.
The game starts again.
You chose door 2 and spent 2 days in Hell. You return to the gates, which have been shuffled, and choose again.
You chose door 1 and spent 1 day in Hell. You return to the gates, which have been shuffled, and choose again.
You chose door 1 and spent 1 day in Hell. You return to the gates, which have been shuffled, and choose again.
You chose door 1 and spent 1 day in Hell. You return to the gates, which have been shuffled, and choose again.
You chose door 2 and spent 2 days in Hell. You return to the gates, which have been shuffled, and choose again.
You chose door 3, which is the right door. Well done, thou good and faithful servant - enter thou into the joy of our Lord.
You've done 4 trials and this time you've spent 7 days in Hell.
The game starts again.
You've done 5 trials and this time you've spent 0 days in Hell.
The game starts again.
You've done 6 trials and this time you've spent 0 days in Hell.
The game starts again.
You've done 7 trials and this time you've spent 0 days in Hell.
The game starts again.
You've done 8 trials and this time you've spent 0 days in Hell.
The game starts again.
You've done 9 trials and this time you've spent 0 days in Hell.
The game starts again.
You chose door 3, which is the right door. Well done, thou good and faithful servant - enter thou into the joy of our Lord.
You've done 10 trials and this time you've spent 0 days in Hell.
...
The game starts again.
You chose door 2 and spent 2 days in Hell. You return to the gates, which have been shuffled, and choose again.
You chose door 2 and spent 2 days in Hell. You return to the gates, which have been shuffled, and choose again.
You chose door 1 and spent 1 day in Hell. You return to the gates, which have been shuffled, and choose again.
You chose door 2 and spent 2 days in Hell. You return to the gates, which have been shuffled, and choose again.
You chose door 1 and spent 1 day in Hell. You return to the gates, which have been shuffled, and choose again.
You chose door 3, which is the right door. Well done, thou good and faithful servant - enter thou into the joy of our Lord.
You've done 20 trials and this time you've spent 8 days in Hell.
The game starts again.
You've done 21 trials and this time you've spent 0 days in Hell.
The game starts again.
You've done 22 trials and this time you've spent 0 days in Hell.
The game starts again.
You've done 23 trials and this time you've spent 0 days in Hell.
The game starts again.
You've done 24 trials and this time you've spent 0 days in Hell.
The game starts again.
You've done 25 trials and this time you've spent 0 days in Hell.
The game starts again.
A total of 25 trials run.
The average number of days needed was 0.88
>
我很少尝试的原因是我经常感觉到一个错误,导致结果为 0。监控变量后,发现代码好像跳过了if迭代:
if choice == 1:
days[0] = 1
print("You chose door 1 and spent 1 day in Hell. You return to the gates, which have been shuffled, and choose again.")
chooseGates()
elif choice == 2:
days[0] = 2
print("You chose door 2 and spent 2 days in Hell. You return to the gates, which have been shuffled, and choose again.")
chooseGates()
else:
print("You chose door 3, which is the right door. Well done, thou good and faithful servant - enter thou into the joy of our Lord.")
例如,以下是一些结果:
You've done 5 trials and this time you've spent 0 days in Hell.
The game starts again.
You've done 6 trials and this time you've spent 0 days in Hell.
The game starts again.
You've done 7 trials and this time you've spent 0 days in Hell.
The game starts again.
You've done 8 trials and this time you've spent 0 days in Hell.
The game starts again.
You've done 9 trials and this time you've spent 0 days in Hell.
Python 如何跳过每次都需要打印某些内容的整个 if 迭代?(里面有 else 陈述句,它必须打印一些东西!)当我只请求 10 次时,它是如何提供 32 次试验的?有人可以帮帮我吗?
uj5u.com热心网友回复:
这段代码怎么样?
import random
import numpy as np
def HeavenOrNot():
days=0
while True:
choice=random.choice([0,1,2])
if choice==0:
break
elif choice==1:
days =1
elif choice==2:
days =2
return days
print('Expected number of days in hell:',np.mean([HeavenOrNot() for i in range(100000)]))
0 评论