-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
100 lines (76 loc) · 3.18 KB
/
test.py
File metadata and controls
100 lines (76 loc) · 3.18 KB
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
import nltk
from nltk.corpus import words
import random
# Download the NLTK word corpus if we haven't already
nltk.download('words')
# Get all English words
english_words = words.words()
# Keep only 5-letter words
five_letter_words = [word.lower() for word in english_words if len(word) == 5]
#print(f"There are {len(five_letter_words)} 5-letter words in the English language.")
#print(five_letter_words[:10]) # Print the first 10 5-letter words
def get_feedback(guess, solution):
feedback = [''] * 5
for i in range(5):
if guess[i] == solution[i]:
feedback[i] = 'G'
elif guess[i] in solution:
feedback[i] = 'Y'
else:
feedback[i] = '-'
return feedback
#def filter_words(word_list, guess, feedback):
green_positions = {i: guess[i] for i in range(5) if feedback[i] == 'G'}
yellow_positions = {i: guess[i] for i in range(5) if feedback[i] == 'Y'}
filtered_words = []
for word in word_list:
match = True
# Check green positions
for pos, char in green_positions.items():
if word[pos] != char:
match = False
break
# Check yellow positions
if match:
for pos, char in yellow_positions.items():
if word[pos] == char or char not in word:
match = False
break
# Check gray positions
if match:
for i in range(5):
if feedback[i] == '-' and guess[i] in word:
match = False
break
if match:
filtered_words.append(word)
return filtered_words
def filter_words(word_list, guess, feedback):
green_positions = {i: guess[i] for i in range(5) if feedback[i] == 'G'}
yellow_positions = {i: guess[i] for i in range(5) if feedback[i] == 'Y'}
filtered_words = []
for word in word_list:
if all(word[pos] == char for pos, char in green_positions.items()) and \
all(word[pos] != char and char in word for pos, char in yellow_positions.items()):
filtered_words.append(word)
return filtered_words
def wordle_solver(word_list):
possible_words = word_list[:]
attempts = 0
while attempts < 6:
guess = random.choice(possible_words) # For simplicity, start with a random guess
# Simulate feedback for demonstration purposes (replace with actual feedback in practice)
# In a real scenario, you would receive feedback after each guess from the game
solution = "swish" # Example solution
feedback = get_feedback(guess, solution)
feedback_str = ''.join(feedback) # Convert feedback list to a string for display
print(f"Attempt {attempts + 1}: {guess} - Feedback: {feedback_str}")
if feedback == ['G'] * 5:
print(f"Solved in {attempts + 1} attempts! Solution: {guess}")
return guess
possible_words = filter_words(possible_words, guess, feedback)
attempts += 1
print("Failed to solve the Wordle.")
return None
# Example usage
wordle_solver(five_letter_words)