atropos/environments/community/word_hunt/trie.py
Abhaykhanna3 b5234d4214 Add Word Hunt environment for training models on 4x4 letter grids
- Trie-based solver, official scoring, normalized rewards
- Configurable token limit and detailed README with dictionary download link
- Removes large Dictionary.txt from tracking and adds ignore rules
- All tests pass and pre-commit hooks are clean
2025-07-28 00:37:36 -05:00

54 lines
1.5 KiB
Python

"""
Trie Data Structure for Word Hunt
"""
class TrieNode:
"""A node in the Trie structure."""
def __init__(self):
self.children = {} # A dictionary of child nodes: {char: TrieNode}
self.is_end_of_word = False
class Trie:
"""
Trie data structure for efficient word and prefix lookups, optimized for the Word Hunt game.
"""
def __init__(self):
"""Initializes the Trie with an empty root node."""
self.root = TrieNode()
def insert(self, word: str):
"""
Inserts a word into the Trie. Assumes word is already uppercase.
"""
node = self.root
for char in word:
if char not in node.children:
node.children[char] = TrieNode()
node = node.children[char]
node.is_end_of_word = True
def is_word(self, word: str) -> bool:
"""
Searches for a complete word in the Trie. Assumes word is already uppercase.
"""
node = self.root
for char in word:
if char not in node.children:
return False
node = node.children[char]
return node.is_end_of_word
def is_prefix(self, prefix: str) -> bool:
"""
Checks if a string is a prefix of any word in the Trie. Assumes prefix is already uppercase.
"""
node = self.root
for char in prefix:
if char not in node.children:
return False
node = node.children[char]
return True