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'" Help me to write this Python program. Make the best algorithm to get 100% correct guesses.
- Assistant
Certainly! To write a Python program that interacts with the Wordle-like API to guess words, you can follow these steps:
-
Understand the API: Based on your instructions, it seems that the API endpoint
https://wordle.votee.dev:8000/random?guess=theft&seed=1234takes two parameters:guess(a word guess) andseed(a unique identifier for the game session). -
Set up the environment: You need a Python environment with HTTP request capabilities. The
requestslibrary will be suitable for this task. -
Algorithm Design: Use a strategy to guess words. One simple but effective strategy is to start with common five-letter words and then use the feedback to refine guesses.
Here's a basic implementation of the program:
import requests def get_feedback(guess, seed): # Call the API with the guess and seed response = requests.get(f'https://wordle.votee.dev:8000/random?guess={guess}&seed={seed}') response.raise_for_status() return response.json() # Feedback from the API def update_possible_words(possible_words, guess, feedback): # Update the list of possible words based on the feedback given new_possible_words = [] for word in possible_words: match = True for i, char in enumerate(feedback): if (char == '2' and word[i] != guess[i]) or \ (char == '1' and (word[i] == guess[i] or guess[i] not in word)) or \ (char == '0' and guess[i] in word): match = False break if match: new_possible_words.append(word) return new_possible_words def load_word_list(): # Load a list of five-letter words from a file or any other source # Here, I'm assuming a file called 'words.txt' with a five-letter word on each line with open('words.txt') as f: words = [line.strip() for line in f if len(line.strip()) == 5] return words def solve_wordle(seed): possible_words = load_word_list() guess = "adieu" # Start with a common five-letter word feedback = get_feedback(guess, seed)["result"] print(f'Initial Guess: {guess}, Feedback: {feedback}') while feedback != "22222": possible_words = update_possible_words(possible_words, guess, feedback) if not possible_words: print("No more possible words!") return guess = possible_words[0] feedback = get_feedback(guess, seed)["result"] print(f'Next Guess: {guess}, Feedback: {feedback}') print(f'Solved! The word is: {guess}') if __name__ == "__main__": game_seed = 1234 # Set your game seed here solve_wordle(game_seed)Explanation:
-
Imports and Helper Functions:
requestslibrary is used to make HTTP GET requests to the Wordle API.get_feedbackmakes a request to the API with the current guess and seed and returns the response.update_possible_wordsnarrows down the list of possible words based on the feedback received.load_word_listloads a list of five-letter words (you need a list of such words stored in a file calledwords.txt).
-
Solving the Wordle Puzzle:
solve_wordleinitiates the guessing with a common five-letter word "adieu".- It continues to refine and guess based on the feedback until the correct word (indicated by feedback "22222") is found.
Considerations:
- Ensure you have a comprehensive list of five-letter words available in 'words.txt'.
- You might want to implement a more sophisticated guessing strategy for better performance.
- This program assumes the feedback from the API is a string of numbers where '2' indicates a correct letter and position, '1' indicates a correct letter but wrong position, and '0' indicates an incorrect letter.
This is a basic implementation, and there's always room for optimization, especially in the guessing strategy.
-