Introduction
You work for a company that sells fonts through their website.
They’d like to show a different sentence each time someone views a font on their website.
To give a comprehensive sense of the font, the random sentences should use all the letters in the English alphabet.
They’re running a competition to get suggestions for sentences that they can use.
You’re in charge of checking the submissions to see if they are valid.
Pangram comes from Greek, παν γράμμα, pan gramma, which means "every letter".
The best known English pangram is:
> The quick brown fox jumps over the lazy dog.
Instructions
Instructions
Your task is to figure out if a sentence is a pangram.
A pangram is a sentence using every letter of the alphabet at least once.
It is case insensitive, so it doesn’t matter if a letter is lower-case (e.g. k) or upper-case (e.g. K).
For this exercise, a sentence is a pangram if it contains each of the 26 letters in the English alphabet.
Dig Deeper
all on lower case
all() on lowercased letters
from string import ascii_lowercase
def is_pangram(sentence):
return all(letter in sentence.lower() for letter in ascii_lowercase)
- This begins by importing all of the ascii_lowercase letters.
- It lowercases the input by using the lower() method.
- It then checks if all letters in the lowercase alphabet are contained in the lowercased
sentence,
using the all() function.
- If all of the letters in the alphabet are contained in the
sentence, then the function will return True.
Instead of `lower()`, the [`casefold`](https://docs.python.org/3/library/stdtypes.html#str.casefold)
method could be used to lowercase the letters.
`casefold()` differs from `lower()` in lowercasing certain Unicode characters.
At the time of writing, those differences are not of concern to this exercise.
Also, `casefold()` benched slower than `lower()`.
set with issubset
set with is_subset
from string import ascii_lowercase
ALPHABET = set(ascii_lowercase)
def is_pangram(sentence):
return ALPHABET.issubset(sentence.lower())
In this approach a set is made from the ascii_lowercase letters,
and another set is made from the lowercased letters in the sentence.
The function returns if the alphabet set issubset() of the sentence set.
If all of the letters in the alphabet are a subset of the letters in the sentence,
then the function will return True.
set with len
set with len()
def is_pangram(sentence):
return len(set(ltr for ltr in sentence.lower() if ltr.isalpha())) == 26
- This approach first makes a set from the
lowercased characters of the sentence.
- The characters are filtered using a set comprehension with an
if isalpha() statement, so that only alphabetic characters make it into the set.
- The function returns whether the
len() of the set is 26.
If the number of unique ASCII (American Standard Code for Information Interchange) letters in the set is equal to the 26 letters in the 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.
Source: Exercism python/pangram