Prompt
Choose efficient algorithms and data structures to avoid unnecessary computational complexity. Look for opportunities to replace expensive operations with more efficient alternatives, especially when dealing with collections and search operations.
Key optimization patterns to apply:
- Avoid O(n) operations when O(1) alternatives exist: Replace
list.pop()with set-based approaches, use appropriate data structures for the task - Use specialized search methods: Prefer
iter_prefix()over full iteration when searching by prefix - Apply short-circuit evaluation: Order boolean conditions to avoid expensive operations in common cases
- Choose appropriate collection types: Use
set()for deduplication instead of manually checking lists, use generators/iterables instead of materializing full lists when possible
Example of inefficient vs efficient approach:
# Inefficient: O(n) list.pop operation
for i, dep in enumerate(dependencies):
if dep.constraint.is_empty():
new_dependencies.append(dependencies.pop(i))
# Efficient: O(1) set-based blacklist approach
blacklist = set()
for dep in dependencies:
if dep.constraint.is_empty():
blacklist.add(dep)
break
# Later: skip blacklisted items
for dep in dependencies:
if dep not in blacklist: # O(1) lookup
process(dep)
Always consider the computational complexity of your chosen approach and whether a more efficient algorithm or data structure could achieve the same result.