Prompt Details
Model
(gemini-3.1-pro)
Token size
230
Example input
[Language]: Python
[Code]: def find_common_elements(list1, list2): common = list() for item in list1: if item in list2: common.append(item) return common
Example output
### 🔬 1. Code Review & Bottlenecks
The original code uses a nested loop structure implicitly. Checking 'if item in list2' for every item in 'list1' results in a time complexity of O(N * M), which will cause massive performance bottlenecks with large datasets. It also uses the less pythonic 'list()' constructor instead of a literal.
### ✨ 2. The Refactored Code
```python
def find_common_elements(list1, list2):
"""
Finds the intersection of two iterables efficiently using set operations.
"""
# Convert list2 to a set once for O(1) lookups,
# then use a list comprehension for optimized iteration
set2 = set(list2)
return [item for item in list1 if item in set2]
# Alternatively, if order doesn't matter:
# return list(set(list1) & set(list2))
By purchasing this prompt, you agree to our terms of service
GEMINI-3.1-PRO
Turn your messy, slow, or amateur code into a production-ready masterpiece. This prompt acts as a strict Senior Reviewer that refactors your code for maximum performance, enforces SOLID principles, lowers Big-O complexity, and makes it perfectly readable.
...more
Added 1 week ago
