-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman_api_user_IG.py
374 lines (293 loc) · 15.8 KB
/
hangman_api_user_IG.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# -*- coding: utf-8 -*-
"""hangman_api_user.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1DlRfqw1QHsa3l9i4ltIFV5x31szgVWC1
# Trexquant Interview Project (The Hangman Game)
* Copyright Trexquant Investment LP. All Rights Reserved.
* Redistribution of this question without written consent from Trexquant is prohibited
## Instruction:
For this coding test, your mission is to write an algorithm that plays the game of Hangman through our API server.
When a user plays Hangman, the server first selects a secret word at random from a list. The server then returns a row of underscores (space separated)—one for each letter in the secret word—and asks the user to guess a letter. If the user guesses a letter that is in the word, the word is redisplayed with all instances of that letter shown in the correct positions, along with any letters correctly guessed on previous turns. If the letter does not appear in the word, the user is charged with an incorrect guess. The user keeps guessing letters until either (1) the user has correctly guessed all the letters in the word
or (2) the user has made six incorrect guesses.
You are required to write a "guess" function that takes current word (with underscores) as input and returns a guess letter. You will use the API codes below to play 1,000 Hangman games. You have the opportunity to practice before you want to start recording your game results.
Your algorithm is permitted to use a training set of approximately 250,000 dictionary words. Your algorithm will be tested on an entirely disjoint set of 250,000 dictionary words. Please note that this means the words that you will ultimately be tested on do NOT appear in the dictionary that you are given. You are not permitted to use any dictionary other than the training dictionary we provided. This requirement will be strictly enforced by code review.
You are provided with a basic, working algorithm. This algorithm will match the provided masked string (e.g. a _ _ l e) to all possible words in the dictionary, tabulate the frequency of letters appearing in these possible words, and then guess the letter with the highest frequency of appearence that has not already been guessed. If there are no remaining words that match then it will default back to the character frequency distribution of the entire dictionary.
This benchmark strategy is successful approximately 18% of the time. Your task is to design an algorithm that significantly outperforms this benchmark.
"""
import json
import requests
import random
import string
import secrets
import time
import re
import collections
import math
try:
from urllib.parse import parse_qs, urlencode, urlparse
except ImportError:
from urlparse import parse_qs, urlparse
from urllib import urlencode
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
class HangmanAPI(object):
def __init__(self, access_token=None, session=None, timeout=None):
self.hangman_url = self.determine_hangman_url()
self.access_token = access_token
self.session = session or requests.Session()
self.timeout = timeout
self.guessed_letters = []
full_dictionary_location = "words_250000_train.txt"
self.full_dictionary = self.build_dictionary(full_dictionary_location)
self.full_dictionary_common_letter_sorted = collections.Counter("".join(self.full_dictionary)).most_common()
self.current_dictionary = []
@staticmethod
def determine_hangman_url():
links = ['https://trexsim.com', 'https://sg.trexsim.com']
data = {link: 0 for link in links}
for link in links:
requests.get(link)
for i in range(10):
s = time.time()
requests.get(link)
data[link] = time.time() - s
link = sorted(data.items(), key=lambda x: x[1])[0][0]
link += '/trexsim/hangman'
return link
def guess(self, word):
clean_word = word[::2].replace("_", ".")
len_word = len(clean_word)
current_dictionary = self.current_dictionary
new_dictionary = []
for dict_word in current_dictionary:
if len(dict_word) != len_word:
continue
if re.match(clean_word, dict_word):
new_dictionary.append(dict_word)
self.current_dictionary = new_dictionary
full_dict_string = "".join(new_dictionary)
c = collections.Counter(full_dict_string)
sorted_letter_count = c.most_common()
max_gain = -1
guess_letter = '!'
for letter, instance_count in sorted_letter_count:
if letter not in self.guessed_letters:
letter_positions = [i for i, l in enumerate(word) if l == letter]
remaining_possible_words = [w for w in new_dictionary if all(w[i] == letter or w[i] == '_' for i in letter_positions)]
letter_counts = collections.Counter(w for w in remaining_possible_words)
total_words = len(remaining_possible_words)
information_gain = 0.0
for count in letter_counts.values():
probability = count / total_words
information_gain -= probability * math.log2(probability)
if information_gain > max_gain:
max_gain = information_gain
guess_letter = letter
if guess_letter == '!':
sorted_letter_count = self.full_dictionary_common_letter_sorted
for letter, instance_count in sorted_letter_count:
if letter not in self.guessed_letters:
guess_letter = letter
break
return guess_letter
# Baseline Algorithm provided by Trexquant
# # clean the word so that we strip away the space characters
# # replace "_" with "." as "." indicates any character in regular expressions
# clean_word = word[::2].replace("_",".")
# # find length of passed word
# len_word = len(clean_word)
# # grab current dictionary of possible words from self object, initialize new possible words dictionary to empty
# current_dictionary = self.current_dictionary
# new_dictionary = []
# # iterate through all of the words in the old plausible dictionary
# for dict_word in current_dictionary:
# # continue if the word is not of the appropriate length
# if len(dict_word) != len_word:
# continue
# # if dictionary word is a possible match then add it to the current dictionary
# if re.match(clean_word,dict_word):
# new_dictionary.append(dict_word)
# # overwrite old possible words dictionary with updated version
# self.current_dictionary = new_dictionary
# # count occurrence of all characters in possible word matches
# full_dict_string = "".join(new_dictionary)
# c = collections.Counter(full_dict_string)
# sorted_letter_count = c.most_common()
# guess_letter = '!'
# # return most frequently occurring letter in all possible words that hasn't been guessed yet
# for letter,instance_count in sorted_letter_count:
# if letter not in self.guessed_letters:
# guess_letter = letter
# break
# # if no word matches in training dictionary, default back to ordering of full dictionary
# if guess_letter == '!':
# sorted_letter_count = self.full_dictionary_common_letter_sorted
# for letter,instance_count in sorted_letter_count:
# if letter not in self.guessed_letters:
# guess_letter = letter
# break
# return guess_letter
##########################################################
# You'll likely not need to modify any of the code below #
##########################################################
def build_dictionary(self, dictionary_file_location):
text_file = open(dictionary_file_location,"r")
full_dictionary = text_file.read().splitlines()
text_file.close()
return full_dictionary
def start_game(self, practice=True, verbose=True):
# reset guessed letters to empty set and current plausible dictionary to the full dictionary
self.guessed_letters = []
self.current_dictionary = self.full_dictionary
response = self.request("/new_game", {"practice":practice})
if response.get('status')=="approved":
game_id = response.get('game_id')
word = response.get('word')
tries_remains = response.get('tries_remains')
if verbose:
print("Successfully start a new game! Game ID: {0}. # of tries remaining: {1}. Word: {2}.".format(game_id, tries_remains, word))
while tries_remains>0:
# get guessed letter from user code
guess_letter = self.guess(word)
# append guessed letter to guessed letters field in hangman object
self.guessed_letters.append(guess_letter)
if verbose:
print("Guessing letter: {0}".format(guess_letter))
try:
res = self.request("/guess_letter", {"request":"guess_letter", "game_id":game_id, "letter":guess_letter})
except HangmanAPIError:
print('HangmanAPIError exception caught on request.')
continue
except Exception as e:
print('Other exception caught on request.')
raise e
if verbose:
print("Sever response: {0}".format(res))
status = res.get('status')
tries_remains = res.get('tries_remains')
if status=="success":
if verbose:
print("Successfully finished game: {0}".format(game_id))
return True
elif status=="failed":
reason = res.get('reason', '# of tries exceeded!')
if verbose:
print("Failed game: {0}. Because of: {1}".format(game_id, reason))
return False
elif status=="ongoing":
word = res.get('word')
else:
if verbose:
print("Failed to start a new game")
return status=="success"
def my_status(self):
return self.request("/my_status", {})
def request(
self, path, args=None, post_args=None, method=None):
if args is None:
args = dict()
if post_args is not None:
method = "POST"
# Add `access_token` to post_args or args if it has not already been
# included.
if self.access_token:
# If post_args exists, we assume that args either does not exists
# or it does not need `access_token`.
if post_args and "access_token" not in post_args:
post_args["access_token"] = self.access_token
elif "access_token" not in args:
args["access_token"] = self.access_token
time.sleep(0.2)
num_retry, time_sleep = 50, 2
for it in range(num_retry):
try:
response = self.session.request(
method or "GET",
self.hangman_url + path,
timeout=self.timeout,
params=args,
data=post_args,
verify=False
)
break
except requests.HTTPError as e:
response = json.loads(e.read())
raise HangmanAPIError(response)
except requests.exceptions.SSLError as e:
if it + 1 == num_retry:
raise
time.sleep(time_sleep)
headers = response.headers
if 'json' in headers['content-type']:
result = response.json()
elif "access_token" in parse_qs(response.text):
query_str = parse_qs(response.text)
if "access_token" in query_str:
result = {"access_token": query_str["access_token"][0]}
if "expires" in query_str:
result["expires"] = query_str["expires"][0]
else:
raise HangmanAPIError(response.json())
else:
raise HangmanAPIError('Maintype was not text, or querystring')
if result and isinstance(result, dict) and result.get("error"):
raise HangmanAPIError(result)
return result
class HangmanAPIError(Exception):
def __init__(self, result):
self.result = result
self.code = None
try:
self.type = result["error_code"]
except (KeyError, TypeError):
self.type = ""
try:
self.message = result["error_description"]
except (KeyError, TypeError):
try:
self.message = result["error"]["message"]
self.code = result["error"].get("code")
if not self.type:
self.type = result["error"].get("type", "")
except (KeyError, TypeError):
try:
self.message = result["error_msg"]
except (KeyError, TypeError):
self.message = result
Exception.__init__(self, self.message)
"""# API Usage Examples
## To start a new game:
1. Make sure you have implemented your own "guess" method.
2. Use the access_token that we sent you to create your HangmanAPI object.
3. Start a game by calling "start_game" method.
4. If you wish to test your function without being recorded, set "practice" parameter to 1.
5. Note: You have a rate limit of 20 new games per minute. DO NOT start more than 20 new games within one minute.
"""
api = HangmanAPI(access_token="80d66eeed132341ff28ea2af32f1ab", timeout=2000)
"""## Playing practice games:
You can use the command below to play up to 100,000 practice games.
"""
for i in range(300):
api.start_game(practice=1,verbose=True)
[total_practice_runs,total_recorded_runs,total_recorded_successes,total_practice_successes] = api.my_status() # Get my game stats: (# of tries, # of wins)
practice_success_rate = total_practice_successes / total_practice_runs
print('run %d practice games out of an allotted 100,000. practice success rate so far = %.3f' % (total_practice_runs, practice_success_rate))
"""## Playing recorded games:
Please finalize your code prior to running the cell below. Once this code executes once successfully your submission will be finalized. Our system will not allow you to rerun any additional games.
Please note that it is expected that after you successfully run this block of code that subsequent runs will result in the error message "Your account has been deactivated".
Once you've run this section of the code your submission is complete. Please send us your source code via email.
"""
for i in range(1000):
print('Playing ', i, ' th game')
# Uncomment the following line to execute your final runs. Do not do this until you are satisfied with your submission
#api.start_game(practice=0,verbose=False)
# DO NOT REMOVE as otherwise the server may lock you out for too high frequency of requests
time.sleep(0.5)
"""## To check your game statistics
1. Simply use "my_status" method.
2. Returns your total number of games, and number of wins.
"""
[total_practice_runs,total_recorded_runs,total_recorded_successes,total_practice_successes] = api.my_status() # Get my game stats: (# of tries, # of wins)
success_rate = total_recorded_successes/total_recorded_runs
print('overall success rate = %.3f' % success_rate)