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 requests import json
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 solve the puzzle
def solve_puzzle(seed): attempts = 0 current_word_list = word_list while attempts < 6: guess = current_word_list[0] # Initially, guess the first word in the 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) else: print("Error: No feedback received. Exiting.") return None attempts += 1 print("Failed to solve the puzzle within 6 attempts.") return NoneExample usage
seed = 1234 # This should be dynamic or user input in a real scenario solve_puzzle(seed)" Can you add the code to test all the answers and calculate the mean guess number? The answer is from the api, no my wordlist.
- Assistant
Certainly! To calculate the mean guess number, we will need to modify your program to include functionality for testing the API-provided answers and computing the average number of guesses required to solve each puzzle.
Here's an updated version of your program that includes this functionality:
import requests import json # 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 solve a single puzzle def solve_puzzle(seed): attempts = 0 current_word_list = word_list while attempts < 6: guess = current_word_list[0] # Initially, guess the first word in the 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 attempts + 1 # Filter the word list based on the feedback current_word_list = filter_word_list(current_word_list, feedback) else: print("Error: No feedback received. Exiting.") return None attempts += 1 print("Failed to solve the puzzle within 6 attempts.") return None # Function to test all the answers and calculate the mean guess number def calculate_mean_guesses(seed_range): total_attempts = 0 solved_count = 0 for seed in seed_range: print(f"Solving puzzle with seed {seed}...") attempts = solve_puzzle(seed) if attempts is not None: total_attempts += attempts solved_count += 1 if solved_count > 0: mean_attempts = total_attempts / solved_count print(f"Solved {solved_count} puzzles.") print(f"Mean number of guesses: {mean_attempts:.2f}") else: print("No puzzles were solved.") # Example usage seed_range = range(1234, 1244) # This range should cover the number of puzzles you want to test calculate_mean_guesses(seed_range)Explanation:
solve_puzzle(seed)function: This function tries to solve a puzzle with a given seed and returns the number of attempts taken to solve the puzzle.calculate_mean_guesses(seed_range)function: This function iterates over a range of seeds, solves each puzzle, and calculates the mean number of guesses required to solve the puzzles.
You can adjust the
seed_rangeto test different sets of puzzles. The example above tests seeds from1234to1243(10 puzzles). Adjust the range according to your needs to cover more or fewer puzzles.