Skip to content

Commit 1a164c7

Browse files
authored
perf: optimize is_pangram by fixing unnecessary list creation
Issue: Current implementation creates an intermediate list from a set, which is redundant and inefficient. **Before:** ```python return len([ltr for ltr in set(sentence.lower()) if ltr.isalpha()]) == 26 ``` **After:** ```python return len(set(ltr for ltr in sentence.lower() if ltr.isalpha())) == 26 ``` Why this is better: - Eliminates double iteration: Old version iterates once for set(), again for list comprehension - Removes unnecessary list creation: No need to convert set → list just to count - Better memory usage: Generator expression feeds directly into set constructor - Same time complexity but more efficient constant factors The old approach first deduplicates all the characters, then filters alphabetic ones. The new approach filters while building the set, which is the natural order of operations for this problem.
1 parent a51a10e commit 1a164c7

File tree

1 file changed

+1
-3
lines changed

1 file changed

+1
-3
lines changed

exercises/practice/pangram/.approaches/introduction.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ For more information, check the [`set` with `issubset()` approach][approach-set-
4242

4343
```python
4444
def is_pangram(sentence):
45-
return len([ltr for ltr in set(sentence.lower()) if ltr.isalpha()]) \
46-
== 26
47-
45+
return len(set(ltr for ltr in sentence.lower() if ltr.isalpha())) == 26
4846
```
4947

5048
For more information, check the [`set` with `len()` approach][approach-set-len].

0 commit comments

Comments
 (0)