作為一個業余 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)]))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/401944.html
