Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions exercises/practice/pangram/.approaches/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"introduction": {
"authors": ["bobahop"]
"authors": ["bobahop"],
"contributors": ["princemuel"]
},
"approaches": [
{
Expand All @@ -22,7 +23,8 @@
"slug": "set-len",
"title": "set with len",
"blurb": "Use set with len.",
"authors": ["bobahop"]
"authors": ["bobahop"],
"contributors": ["princemuel"]
},
{
"uuid": "0a6d1bbf-6d60-4489-b8d9-b8375894628b",
Expand Down
4 changes: 1 addition & 3 deletions exercises/practice/pangram/.approaches/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ For more information, check the [`set` with `issubset()` approach][approach-set-

```python
def is_pangram(sentence):
return len([ltr for ltr in set(sentence.lower()) if ltr.isalpha()]) \
== 26

return len(set(ltr for ltr in sentence.lower() if ltr.isalpha())) == 26
```

For more information, check the [`set` with `len()` approach][approach-set-len].
Expand Down
16 changes: 7 additions & 9 deletions exercises/practice/pangram/.approaches/set-len/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

```python
def is_pangram(sentence):
return len([ltr for ltr in set(sentence.lower()) if ltr.isalpha()]) \
== 26

return len(set(ltr for ltr in sentence.lower() if ltr.isalpha())) == 26
```

- This approach first makes a [set][set] from the [`lower`][lower]cased characters of the `sentence`.
- The characters in the `set`are then iterated in a [list comprehension][list-comprehension].
- The characters are filtered by an `if` [`isalpha()`][isalpha] statement, so that only alphabetic characters make it into the list.
- The function returns whether the [`len()`][len] of the [`list`][list] is `26`.
If the number of unique letters in the `set` is equal to the `26` letters in the alphabet, then the function will return `True`.
- 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.
- The function returns whether the [`len()`][len] of the [`set`][set] is `26`.
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`.
- This approach is efficient because it uses a set to eliminate duplicates and directly checks the length, which is a constant time operation.

[lower]: https://docs.python.org/3/library/stdtypes.html?#str.lower
[set]: https://docs.python.org/3/library/stdtypes.html?#set
[list-comprehension]: https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions
[set-comprehension]: https://realpython.com/python-set-comprehension/#introducing-set-comprehensions
[isalpha]: https://docs.python.org/3/library/stdtypes.html?highlight=isalpha#str.isalpha
[len]: https://docs.python.org/3/library/functions.html?#len
[list]: https://docs.python.org/3/library/stdtypes.html?#list
[ascii]: https://en.wikipedia.org/wiki/ASCII
3 changes: 1 addition & 2 deletions exercises/practice/pangram/.approaches/set-len/snippet.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
def is_pangram(sentence):
return len([ltr for ltr in set(sentence.lower()) if ltr.isalpha()]) \
== 26
return len(set(ltr for ltr in sentence.lower() if ltr.isalpha())) == 26
3 changes: 2 additions & 1 deletion exercises/practice/pangram/.articles/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"slug": "performance",
"title": "Performance deep dive",
"blurb": "Deep dive to find out the most performant approach to determining a pangram.",
"authors": ["bobahop"]
"authors": ["bobahop"],
"contributors": ["princemuel"]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def is_pangram(sentence):
val = timeit.timeit("""is_pangram("Victor jagt zwölf_(12) Boxkämpfer quer über den großen Sylter Deich.")""",
"""
def is_pangram(sentence):
return len([ltr for ltr in set(sentence.lower()) if ltr.isalpha()]) == 26
return len(set(ltr for ltr in sentence.lower() if ltr.isalpha())) == 26

""", number=loops) / loops

Expand Down
16 changes: 8 additions & 8 deletions exercises/practice/pangram/.articles/performance/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ For our performance investigation, we'll also include a fourth approach that [us
To benchmark the approaches, we wrote a [small benchmark application][benchmark-application] using the [`timeit`][timeit] library.

```
all: 1.505466179997893e-05
all: 1.6063886400021147e-05 // with sentence.casefold()
set: 1.950172399985604e-06
len: 3.7158977999933994e-06
bit: 8.75982620002469e-06
all: 1.8692991019000146e-05
all: 1.686682232399926e-05 // with sentence.casefold()
set: 2.5181135439997888e-06
len: 5.848111433000668e-06
bit: 1.2118699087000096e-05
```

- The `set` `len()` approach is not as fast as the `set` `issubset()` approach.
- The `all()` approach is slower than either `set` approach.
Using `casefold` was slower than using `lower`.
- The `all()` approach is significantly slower than either `set` approach (approximately 6-8x slower).
Using `casefold()` versus `lower()` showed variable performance, with each being faster in different runs.
- Although the bit field approach may be faster in other languages, it is significantly slower in Python.
It is faster than the `all()` approach, but much slower than either `set` approach.
It is faster than the `all()` approach, but much slower than either `set` approach.

[benchmark-application]: https://github.com/exercism/python/blob/main/exercises/practice/pangram/.articles/performance/code/Benchmark.py
[timeit]: https://docs.python.org/3/library/timeit.html
Expand Down
10 changes: 5 additions & 5 deletions exercises/practice/pangram/.articles/performance/snippet.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
```
all: 1.505466179997893e-05
all: 1.6063886400021147e-05 // with sentence.casefold()
set: 1.950172399985604e-06
len: 3.7158977999933994e-06
bit: 8.75982620002469e-06
all: 1.8692991019000146e-05
all: 1.686682232399926e-05 // with sentence.casefold()
set: 2.5181135439997888e-06
len: 5.848111433000668e-06
bit: 1.2118699087000096e-05
```
Loading