The instructions for the coding test are as follows:" Votee would like to conduct the…
- User
The instructions for the coding test are as follows:"
Votee would like to conduct the coding test in a recorded live session. The idea is to connect to an API that plays a Wordle-like puzzle. You can find the API documentation at the following link: https://wordle.votee.dev:8000/redoc
Developers are allowed to use any frameworks, IDEs, chatbots, LLMs, or other resources to write their program and showcase their skill set. However, if you take references from other open-source projects or tutorials, please ensure that proper credit is given. Plagiarism will not be tolerated, and it may affect the evaluation of your application.
Your task is to write a program that automatically guesses random words against the API, using the following command as an example: curl -v ‘https://wordle.votee.dev:8000/random?guess=theft&seed=1234'" If I run curl -v ‘https://wordle.votee.dev:8000/random?guess=theft&seed=1234 in the terminal, the output: StatusCode : 200 StatusDescription : OK Content : [{"slot":0,"guess":"t","result":"correct"},{"slot":1,"guess":"h","result":"correct"},{"slot":2,"guess":"e","result":"correct"}, {"slot":3,"guess":"f","result":"correct"},{"slot":4,"guess":"t","result":... RawContent : HTTP/1.1 200 OK Content-Length: 211 Content-Type: application/json Date: Sun, 26 May 2024 08:27:33 GMT Server: uvicorn
[{"slot":0,"guess":"t","result":"correct"},{"slot":1,"guess":"h","result":...Forms : {} Headers : {[Content-Length, 211], [Content-Type, application/json], [Date, Sun, 26 May 2024 08:27:33 GMT], [Server, uvicorn]} Images : {} InputFields : {} Links : {} ParsedHtml : mshtml.HTMLDocumentClass RawContentLength : 211
My Python program that guess the words: " import random import requests import json from collections import Counter
Load the word list
with open("C:/Users/a/Desktop/valid-wordle-words.txt", 'r') as file: word_list = [line.strip() for line in file.readlines()]
API base URL
base_url = 'https://wordle.votee.dev:8000/random'
Function to make a guess
def make_guess(guess, seed): response = requests.get(f"{base_url}?guess={guess}&seed={seed}") if response.status_code == 200: return response.json() else: print("Failed to make a guess.") return None
Function to filter the word list based on feedback
def filter_word_list(word_list, feedback): new_word_list = word_list[:] for feedback_item in feedback: slot = feedback_item['slot'] guess = feedback_item['guess'] result = feedback_item['result']
if result == 'correct': new_word_list = [word for word in new_word_list if word[slot] == guess] elif result == 'present': new_word_list = [word for word in new_word_list if guess in word and word[slot] != guess] elif result == 'absent': new_word_list = [word for word in new_word_list if guess not in word] return new_word_listFunction to choose the initial guess based on letter frequency and no repeats
def choose_initial_guess(word_list): # Combine all words into a single string to count letter frequency all_letters = ''.join(word_list) letter_freq = Counter(all_letters)
# Sort words by the sum of their letter frequencies words_sorted_by_freq = sorted(word_list, key=lambda word: sum(letter_freq[char] for char in set(word)), reverse=True) # Choose the first word that has no repeating letters for word in words_sorted_by_freq: if len(set(word)) == len(word): return word # As a fallback, return the first word in the original list return word_list[0]Function to solve the puzzle
def solve_puzzle(seed): attempts = 0 current_word_list = word_list print(f"Words remaining in the list: {len(current_word_list)}") initial_guess = choose_initial_guess(current_word_list)
while attempts < 6: if attempts == 0: guess = initial_guess else: guess = current_word_list[0] # Choose the first word in the filtered list print(f"Attempt {attempts + 1}: Guessing '{guess}'...") # Make the guess by calling the API feedback = make_guess(guess, seed) # Check if the API call was successful and we received feedback if feedback: # Print the feedback for the current guess print(f"Feedback: {json.dumps(feedback, indent=2)}") # Check if all letters are correct if all(item['result'] == 'correct' for item in feedback): print(f"Solved with guess: {guess}") return guess # Filter the word list based on the feedback current_word_list = filter_word_list(current_word_list, feedback) print(f"Words remaining in the list: {len(current_word_list)}") else: print("Error: No feedback received. Exiting.") return None attempts += 1 print("Failed to solve the puzzle within 6 attempts.") return NoneExample usage
seed = random.randint(0, 1000) # This should be dynamic or user input in a real scenario solve_puzzle(seed) " Can you make the filtering algorithm better by keep tracking of two lists of words. One is going to be the answer space, which contains the potential remaining words after it’s been filtered with each guess. There’s going to be a second word list, which is every single word in the dictionary that never gets filtered. What we’re going to do is build our letter frequencies to determine which words we should favor and try to pick. That’ll be our answer space, which continues to get filtered. But the guess we’re going to make is from all words, so we’re going to pick a word that can get us more of these letters at a single time and not even worry about letters we’ve already picked. The whole goal is to filter the answer space down to a single word and then finally guess it.
- Assistant
Certainly! Let's adjust the solution to incorporate the modified approach. This will involve maintaining two lists: the answer space and the full word list. We'll use the answer space to filter possible answers while making our guesses from the full word list to maximize the usefulness of each guess.
Here's the refactored code:
import random import requests import json from collections import Counter # Load the word list with open("C:/Users/a/Desktop/valid-wordle-words.txt", 'r') as file: word_list = [line.strip() for line in file.readlines()] # API base URL base_url = 'https://wordle.votee.dev:8000/random' # Function to make a guess def make_guess(guess, seed): response = requests.get(f"{base_url}?guess={guess}&seed={seed}") if response.status_code == 200: return response.json() else: print("Failed to make a guess.") return None # Function to filter the word list based on feedback def filter_word_list(word_list, feedback): new_word_list = word_list[:] for feedback_item in feedback: slot = feedback_item['slot'] guess = feedback_item['guess'] result = feedback_item['result'] if result == 'correct': new_word_list = [word for word in new_word_list if word[slot] == guess] elif result == 'present': new_word_list = [word for word in new_word_list if guess in word and word[slot] != guess] elif result == 'absent': new_word_list = [word for word in new_word_list if guess not in word] return new_word_list # Function to build letter frequency for the current answer space def build_letter_frequency(word_list): all_letters = ''.join(word_list) return Counter(all_letters) # Function to choose the best guess def choose_best_guess(answer_space, full_word_list): letter_freq = build_letter_frequency(answer_space) # Sort words by the sum of their letter frequencies words_sorted_by_freq = sorted(full_word_list, key=lambda word: sum(letter_freq[char] for char in set(word)), reverse=True) # Choose the first word that has no repeating letters as a guess for word in words_sorted_by_freq: if len(set(word)) == len(word): return word # As a fallback, return the first word in the original list return words_sorted_by_freq[0] # Function to solve the puzzle def solve_puzzle(seed): attempts = 0 answer_space = word_list[:] # This is the list that will be filtered full_word_list = word_list[:] # This is the full dictionary that never gets filtered while attempts < 6: if attempts == 0: guess = choose_best_guess(answer_space, full_word_list) else: guess = choose_best_guess(answer_space, full_word_list) # Continue picking based on the full list print(f"Attempt {attempts + 1}: Guessing '{guess}'...") # Make the guess by calling the API feedback = make_guess(guess, seed) # Check if the API call was successful and we received feedback if feedback: # Print the feedback for the current guess print(f"Feedback: {json.dumps(feedback, indent=2)}") # Check if all letters are correct if all(item['result'] == 'correct' for item in feedback): print(f"Solved with guess: {guess}") return guess # Filter the answer space based on the feedback answer_space = filter_word_list(answer_space, feedback) print(f"Words remaining in the answer space: {len(answer_space)}") else: print("Error: No feedback received. Exiting.") return None attempts += 1 print("Failed to solve the puzzle within 6 attempts.") return None # Example usage seed = random.randint(0, 1000) # This should be dynamic or user input in a real scenario solve_puzzle(seed)Key Changes:
-
Maintaining Two Lists:
answer_spaceis continuously filtered based on feedback.full_word_listremains static and is used for selecting the best guess.
-
Choosing the Best Guess:
- Always selects the best guess from
full_word_listbased on letter frequencies derived fromanswer_space.
- Always selects the best guess from
-
Initial and Subsequent Guesses:
- Uses
choose_best_guessfor both initial and subsequent guesses, always trying to maximize information gained.
- Uses
This approach ensures that the guesses maximize the information extracted from each attempt, helping to quickly zero in on the correct word.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.