I was trying to optimise a functionfor Hangman code and failed after various attempts, Can anyone please help me understatnd how to solve ths question.
Question:
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 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.
Code:
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 = []\
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()
guess_letter = '!'
for letter,instance_count in sorted_letter_count:
if letter not in self.guessed_letters:
guess_letter = letter
break
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
What I have tried:
I tried to keep a track of correct number of alphabets guessed, and if no right letter is guessed, the algorthm will start creatng "new_dictionary" from full_dictionary again.
I also tried to repeat the same steps no letters is left in "new_dictionary", bbuut still no result.
Can you please help me in finding the optimised algorithm of this function. Thank you.