【发布时间】:2022-01-02 12:11:24
【问题描述】:
作为一名业余 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-iteration:
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 次试验的?有人可以帮帮我吗?
【问题讨论】:
-
递归结构好像不对;要么修复它,要么只使用一个循环?
-
另外,这个问题似乎非常适合分析解决方案......
-
感谢您的评论。您能否指出递归结构的问题?我试着在我的脑海中运行它,它应该可以工作......谢谢
-
提示:内部函数调用返回,外部函数调用恢复后会发生什么?
-
@Jiří Baum 哦,现在我明白了。外部函数将无限重复。
标签: python google-chrome debugging simulation