Created
January 23, 2022 14:40
-
-
Save anabarasan/16ceb6b3cf021f8449d447470e43fad7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
class Wordle: | |
def __init__(self, test=False): | |
self.wordlist = None | |
# wordle_alpha.txt obtained from https://github.com/dwyl/english-words/blob/master/words_alpha.txt | |
with open("words_alpha.txt") as wordle_file: | |
wordlist = wordle_file.read().split("\n") | |
word_length = int(input("Word Length : ")) | |
self.wordlist = [word for word in wordlist if len(word) == word_length] | |
self.wordlist.sort() | |
self.reset_input() | |
if not test: | |
self.repl() | |
def reset_input(self): | |
self.allowed_chars = set() | |
self.contains = set() | |
self.startswith = None | |
self.endswith = None | |
self.positions = [] | |
def repl(self): | |
command = "" | |
while command != "exit": | |
if command == "random": | |
print(f"Random Word! {random.choice(self.wordlist)}") | |
if command == "search": | |
self.get_user_input() | |
# print(self.allowed_chars) | |
filtered_word_list = self.search() | |
print("\n".join(filtered_word_list)) | |
print(f"no of words: {len(filtered_word_list)}") | |
command = input(">>> ") | |
def get_user_input(self): | |
self.reset_input() | |
allowed_chars = input("Allowed Chars : ") | |
self.allowed_chars = set(allowed_chars) | |
contains = input("Containing : ") | |
self.contains = set(contains) | |
self.startswith = input("Starts with : ") | |
self.endswith = input("Ends with : ") | |
self.positions = [] | |
while True: | |
ip = input("Positions : ") | |
if not ip: | |
break | |
ip = ip.split(",") | |
if len(ip) != 2: | |
print("ERROR: Invalid Input.\nPositions : <position>,<character>") | |
continue | |
pos, char = ip | |
pos = int(pos) | |
char = char.strip() | |
self.positions.append((pos, char)) | |
def search(self): | |
filtered_word_list = [] | |
for word in self.wordlist: | |
checks = [True, True, True, True, True] | |
# if contains and set(word) - set("abcdefghijklmnopqrstuvwxyz"): | |
if self.allowed_chars and set(word) - self.allowed_chars: | |
checks[0] = False | |
if self.contains and self.contains - set(word): | |
checks[1] = False | |
if self.startswith and not word.startswith(self.startswith): | |
checks[2] = False | |
if self.endswith and not word.endswith(self.endswith): | |
checks[3] = False | |
for pos, char in self.positions: | |
if not word[pos-1] == char: | |
checks[4] = False | |
break | |
if all(checks): | |
filtered_word_list.append(word) | |
return filtered_word_list | |
if __name__ == "__main__": | |
Wordle() | |
# w = Wordle(True) | |
# w.allowed_chars = set("qwertyuopadfghjklzxcvnm") | |
# w.contains = set("al") | |
# filtered_word_list = w.search() | |
# print("bails" in filtered_word_list) | |
# print(f"no of words: {len(filtered_word_list)}") | |
# print(filtered_word_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment