Home > Software engineering >  python - debugging a Google interview question simulation - '3 doors to heaven simulation'
python - debugging a Google interview question simulation - '3 doors to heaven simulation'

Time:01-02

As an amateur Python user, I'm using a Python code to simulate the classic tech company interview question:

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?

I've written a Python code to simulate this:

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])

The trouble is, this is what Programmiz Python Online Compiler gave me:

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
> 

The reason I made so few trials was that I sensed an error that caused the result to be 0 very often. After monitoring the variables, I found that the code seems to be skipping the 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.")

For example, here are some of the results:

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. 

How did Python skip the whole if-iteration that required something to be printed every time? (With the else-statement inside, it has to print something!) And how did it give 32 trials when I requested only 10? Can someone please help me?

CodePudding user response:

How about this code?

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)]))
  • Related