54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
from collections import Counter
|
|
import urllib.request
|
|
|
|
|
|
def load_dictionary():
|
|
"""Download and return a clean set of valid English words."""
|
|
url = "https://raw.githubusercontent.com/raun/Scrabble/master/words.txt"
|
|
with urllib.request.urlopen(url) as response:
|
|
words = response.read().decode("utf-8").splitlines()
|
|
|
|
# Filter out proper nouns/short words and normalize to lowercase
|
|
return [
|
|
word.strip().lower()
|
|
for word in words
|
|
if word.isalpha() and 2 <= len(word) <= 9
|
|
]
|
|
|
|
|
|
def solve_countdown(letters, dictionary):
|
|
"""Find the longest valid words that can be formed from the input letters."""
|
|
letters_count = Counter(letters.lower())
|
|
valid_words = []
|
|
|
|
for word in dictionary:
|
|
# Check if word letter frequencies fit within the available letters
|
|
if not (Counter(word) - letters_count):
|
|
valid_words.append(word)
|
|
|
|
# Sort by word length (descending), then alphabetically
|
|
valid_words.sort(key=lambda w: (-len(w), w))
|
|
return valid_words
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print("Loading word list...")
|
|
dictionary = load_dictionary()
|
|
|
|
rack = input("\nEnter your letters (e.g., 'mdaisuteo'): ").strip()
|
|
matches = solve_countdown(rack, dictionary)
|
|
|
|
if not matches:
|
|
print("No valid words found.")
|
|
else:
|
|
max_len = len(matches[0])
|
|
print(f"\nLongest word length: {max_len} letters")
|
|
print("--- Top Matches ---")
|
|
|
|
# Group and display top results by length
|
|
current_len = 0
|
|
for word in matches[:20]: # Show top 20 matches
|
|
if len(word) != current_len:
|
|
current_len = len(word)
|
|
print(f"\n{current_len}-letter words:")
|
|
print(f" • {word}") |