You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Pangram]: fix redundant double iteration in dig deeper solution (exercism#3948)
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
Reviewed-by: bethanyg
* docs(pangram): improve approach's content and links
* docs(pangram): improve approach's content and links
* docs(pangram): improve performance content and links
* Added princemuel as contributor
---------
Co-authored-by: BethanyG <[email protected]>
Copy file name to clipboardExpand all lines: exercises/practice/pangram/.approaches/set-len/content.md
+7-9Lines changed: 7 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,20 +2,18 @@
2
2
3
3
```python
4
4
defis_pangram(sentence):
5
-
returnlen([ltr for ltr inset(sentence.lower()) if ltr.isalpha()]) \
6
-
==26
7
-
5
+
returnlen(set(ltr for ltr in sentence.lower() if ltr.isalpha())) ==26
8
6
```
9
7
10
8
- This approach first makes a [set][set] from the [`lower`][lower]cased characters of the `sentence`.
11
-
- The characters in the `set`are then iterated in a [list comprehension][list-comprehension].
12
-
- The characters are filtered by an `if`[`isalpha()`][isalpha] statement, so that only alphabetic characters make it into the list.
13
-
- The function returns whether the [`len()`][len] of the [`list`][list] is `26`.
14
-
If the number of unique letters in the `set` is equal to the `26` letters in the alphabet, then the function will return `True`.
9
+
- The characters are filtered using a [set comprehension][set-comprehension] with an `if`[`isalpha()`][isalpha] statement, so that only alphabetic characters make it into the set.
10
+
- The function returns whether the [`len()`][len] of the [`set`][set] is `26`.
11
+
If the number of unique [ASCII][ascii] (American Standard Code for Information Interchange) letters in the `set` is equal to the `26` letters in the [ASCII][ascii] alphabet, then the function will return `True`.
12
+
- This approach is efficient because it uses a setto eliminate duplicates and directly checks the length, which is a constant time operation.
0 commit comments