Last active
June 18, 2022 19:00
-
-
Save neizod/eeca1a82f1a59a37aa2b702079a7cb5c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from random import randrange, shuffle, choice | |
from collections import Counter | |
class Judge(object): | |
def __init__(self): | |
self.doors = ['goat', 'goat', 'CAR'] | |
shuffle(self.doors) # move the prize into a random door | |
def open_wrong_door(self, select): | |
avail = [i for i, d in enumerate(self.doors) if d == 'goat'] # don't open the correct door | |
if select in avail: | |
avail.remove(select) # don't open the door that player currently select | |
return choice(avail) | |
class Player(object): | |
def __init__(self): | |
self.doors = ['?', '?', '?'] # player did not know which door has the prize | |
self.select = None | |
def select_available_door(self): | |
avail = [i for i, d in enumerate(self.doors) if d == '?'] # only guess unopened door | |
self.select = choice(avail) # random a door that may have prize | |
def change_door(self): | |
avail = [i for i, d in enumerate(self.doors) if d == '?'] # only guess unopened door | |
avail.remove(self.select) # don't select the selected door again | |
self.select = choice(avail) # practically it is the only door left | |
def acknowledge_wrong_door(self, judge): | |
index = judge.open_wrong_door(self.select) | |
self.doors[index] = 'goat' | |
def confirm_selected_door(self, judge): | |
return judge.doors[self.select] | |
def strategy_stay(): | |
judge = Judge() | |
player = Player() | |
player.select_available_door() | |
player.acknowledge_wrong_door(judge) | |
return player.confirm_selected_door(judge) | |
def strategy_rerandom(): | |
judge = Judge() | |
player = Player() | |
player.select_available_door() | |
player.acknowledge_wrong_door(judge) | |
player.select_available_door() | |
return player.confirm_selected_door(judge) | |
def strategy_change(): | |
judge = Judge() | |
player = Player() | |
player.select_available_door() | |
player.acknowledge_wrong_door(judge) | |
player.change_door() | |
return player.confirm_selected_door(judge) | |
n = 100000 | |
print('Stay on the first door') | |
print('======================') | |
for k, v in sorted(Counter(strategy_stay() for _ in range(n)).items()): | |
print(f'{k:4} {100*v/n:6.2f}%') | |
print() | |
print('Random the door again') | |
print('=====================') | |
for k, v in sorted(Counter(strategy_rerandom() for _ in range(n)).items()): | |
print(f'{k:4} {100*v/n:6.2f}%') | |
print() | |
print('Change the door') | |
print('===============') | |
for k, v in sorted(Counter(strategy_change() for _ in range(n)).items()): | |
print(f'{k:4} {100*v/n:6.2f}%') | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment