-
Notifications
You must be signed in to change notification settings - Fork 0
/
100Doors.py
271 lines (212 loc) · 6.93 KB
/
100Doors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import random
# Simply picks the winning door at random
def door_picker(doors):
"""
:param int: doors:
:param doors:
"""
winner = random.randrange(1, doors + 1)
return winner
# This opens all the other doors and allows the user to switch or stay
def door_opener(doors, choice, winner, switch, enable_auto):
"""
:param int: doors:
:param choice: param winner:
:param switch: param enable_auto:
:param doors:
:param winner:
:param enable_auto:
"""
if enable_auto == "n":
switch = None
if choice == winner:
closed_door = random.randrange(1, doors + 1)
while closed_door == winner:
closed_door = random.randrange(1, doors + 1)
else:
closed_door = choice
print("I have opened all but doors " + str(closed_door) + " and " +
str(winner))
if enable_auto == "n":
while not (switch == "y" or switch == "n"):
switch = input("Would you like to switch?(y\\n): ").lower()
if switch == "y":
if choice == winner:
choice = closed_door
else:
choice = winner
return choice, switch
# This is the end game. Displays if the player won or lost
def show_winner(choice, winner, switch):
"""
:param choice: param winner:
:param switch:
:param winner:
"""
if switch == "n":
print("You did not switch and you ", end="")
else:
print("You switched and you ", end="")
if choice == winner:
print("won!")
return 1
else:
print("lost.")
return 0
# Calculates the amount of games won vs played and your % of wins
def show_rate(wins, games):
"""
:param wins: param games:
:param games:
"""
rate = wins / games
print("\n" + str(wins) + " wins of " + str(games) + " games")
print("You are winning " + str(rate * 100) + "% of the time.\n\n")
def shuffle_boxes():
""" """
prisoners_numbers = [x for x in range(0, 100)]
boxes = {}
random.shuffle(prisoners_numbers)
for i, p in enumerate(prisoners_numbers):
boxes[i] = p
return boxes
def random_strategy(prisoner_number, boxes, number_loop):
"""
:param prisoner_number: param boxes:
:param number_loop:
:param boxes:
"""
for k in range(0, 50):
if prisoner_number == boxes[k]:
return True
return False
def try_strategy(boxes, strategy, number_loops):
"""
:param boxes: param strategy:
:param number_loops:
:param strategy:
"""
n_correct = 0
for i in range(0, 100):
correct = strategy(i, boxes, number_loops)
if correct:
n_correct += 1
return n_correct
def loop_strategy(prisoner_number, boxes, number_loops):
"""
:param prisoner_number: param boxes:
:param number_loops:
:param boxes:
"""
next_box = prisoner_number
for x in range(number_loops):
if boxes[next_box] == prisoner_number:
return True
next_box = boxes[next_box]
return False
def prisoners_simulation(total_games, number_loops, strategy):
"""
:param total_games: param number_loops:
:param strategy:
:param number_loops:
"""
wins = 0
results = {}
num_sim = total_games
for sim in range(num_sim):
boxes = shuffle_boxes()
if strategy == 1:
n_correct = try_strategy(boxes, random_strategy, number_loops)
results[sim] = n_correct
elif strategy == 2:
n_correct = try_strategy(boxes, loop_strategy, number_loops)
results[sim] = n_correct
for sim in range(num_sim):
if results[sim] == 100:
wins = wins + 1
return wins
def load_monty_hall():
""" """
wins = 0
games = 0
total_games = "0"
switch = "0"
enable_auto = None
keep_playing = "y"
doors = "0"
while not (doors.isdigit() and 2 < int(doors)):
doors = input("How many doors would you like to play with? ")
doors = int(doors)
while not (enable_auto == "y" or enable_auto == "n"):
enable_auto = input("Would you like to see autoplay? (y\\n): ").lower()
if enable_auto == "y":
while not (switch == "y" or switch == "n"):
switch = input("Always switch doors? (y\\n): ")
while not (total_games.isdigit() and 0 < int(total_games)):
total_games = input("How many games?: ")
while keep_playing == "y":
choice = "0"
if enable_auto == "y":
choice = str(random.randrange(1, doors + 1))
print("There are " + str(doors) +
" doors in front of you.\nOne contains a prize.\n")
if enable_auto == "n":
while not (choice.isdigit() and 0 < int(choice) < doors + 1):
choice = input("Pick one: ")
winner = door_picker(doors)
choice, switch = door_opener(doors, int(choice), winner, switch,
enable_auto)
wins += show_winner(int(choice), winner, switch)
games += 1
show_rate(wins, games)
if enable_auto == "n":
keep_playing = None
while not (keep_playing == "y" or keep_playing == "n"):
keep_playing = input(
"Would you like to keep playing? (y\\n): ").lower()
elif int(total_games) == games:
keep_playing = "n"
def load_prisoners_problem():
""" """
total_games = "0"
while not (total_games.isdigit() and 0 < int(total_games)):
total_games = input("How many games?: ")
total_games = int(total_games)
print(
'\nChoose a strategy you want to simulate the "100 Prisoners Problem":'
)
print("1. Random Strategy")
print("2. Loop Strategy")
options = [1, 2]
strategy = "0"
while not (strategy.isdigit() and int(strategy) in options):
strategy = input("Select the strategy: ")
strategy = int(strategy)
if strategy == 1:
print("Simulating....")
prisoners_wins = prisoners_simulation(total_games, 0, strategy)
show_rate(prisoners_wins, total_games)
elif strategy == 2:
number_loops = input("Enter the number of loops? (default: 50): ")
number_loops = int(number_loops or 50)
print("You have entered " + str(number_loops) + " number of loops.")
print("Simulating...")
prisoners_wins = prisoners_simulation(total_games, number_loops,
strategy)
show_rate(prisoners_wins, total_games)
def main():
""" """
print("Choose a problem to play:")
print("1. Monty Hall Problem")
print("2. 100 Prisoners Problem")
options = [1, 2]
choice = "0"
while not (choice.isdigit() and int(choice) in options):
choice = input("Enter the number of the problem you want to play: ")
choice = int(choice)
if choice == 1:
load_monty_hall()
elif choice == 2:
load_prisoners_problem()
if __name__ == "__main__":
main()