Dig Deeper
Scrub with a list comprehension
Scrub with a list comprehension
def is_isogram(phrase):
scrubbed = [ltr.lower() for ltr in phrase if ltr.isalpha()]
return len(set(scrubbed)) == len(scrubbed)
For this approach, a list comprehension is used to iterate the letters in the input phrase string.
- In the code example,
ltr is the name given to each letter iterated in the for loop.
- The result of each iteration is
ltr.lower(), which is the lowercased letter being iterated.
All of the letters are lowercased so that letters of different cases will become the same letter for comparison purposes,
since A and a are considered to be the same letter.
- The iterable part of the list comprehension is the input phrase.
- The letters are filtered by the use of the optional conditional logic:
if ltr.isalpha().
isalpha() returns True if the letter being iterated is alphabetic.
When the list comprehension is done, the scrubbed variable will be a list holding only lowercased alphabetic characters.
- A
set is constructed from the scrubbed list and its len is compared with the len of the the scrubbed list.
Since a set holds only unique values, the phrase will be an isogram if its number of unique letters is the same as its total number of letters.
The function returns whether the number of unique letters equals the total number of letters.
- For
Alpha it would return False, because a is considered to repeat A, so the number of unique letters in Alpha is 4,
and the total number of letters in Alpha is 5.
- For
Bravo it would return True, since the number of unique letters in Bravo is 5, and the total number of letters in Bravo is 5.
Scrub with a series of replace calls
Scrub with replace()
def is_isogram(phrase):
scrubbed = phrase.replace('-', '').replace(' ', '').lower()
return len(scrubbed) == len(set(scrubbed))
For this approach, replace() is called a couple times to scrub the input phrase string.
Thw two replace() calls are chained, so the output of the first replace() is the input for the next replace().
The output of the last replace() is the input for lower().
All of the letters are lowercased so that letters of different cases will become the same letter for comparison purposes,
since A and a are considered to be the same letter.
When the replacing and lowercasing is done, the scrubbed variable will be a string having no hyphens or spaces,
and with all alphabetic letters lowercased.
- A
set is constructed from the scrubbed string and its len is compared with the len of the the scrubbed string.
Since a set holds only unique values, the phrase will be an isogram if its number of unique letters is the same as its total number of letters.
The function returns whether the number of unique letters equals the total number of letters.
- For
Alpha it would return False, because a is considered to repeat A, so the number of unique letters in Alpha is 4,
and the total number of letters in Alpha is 5.
- For
Bravo it would return True, since the number of unique letters in Bravo is 5, and the total number of letters in Bravo is 5.
Scrub with a regex
Scrub with re.sub()
import re
def is_isogram(phrase):
scrubbed = re.compile('[^a-zA-Z]').sub('', phrase).lower()
return len(set(scrubbed)) == len(scrubbed)
For this approach, regular expression, also known as a regex, is used to scrub the input phrase string.
- In the pattern of
[^a-zA-Z] the brackets are used to define a character set that looks for characters which are not a through z and A through Z.
If the first character of a character set is `^`, all the characters that are _not_ in the rest of the character set will be matched.
This essentially matches any characters which are not in the English alphabet.
The pattern is passed to the compile() method to construct a regular expression object.
- The
sub() method is then called on the regex object.
The sub() method replaces all non-alphabetic characters in the input phrase with an empty string.
- The output of
sub() is then chained as the input for lower().
All of the letters are lowercased so that letters of different cases will become the same letter for comparison purposes,
since A and a are considered to be the same letter.
When the replacing and lowercasing is done, the scrubbed variable will be a string having all alphabetic letters lowercased.
- A
set is constructed from the scrubbed string and its len is compared with the len of the the scrubbed string.
Since a set holds only unique values, the phrase will be an isogram if its number of unique letters is the same as its total number of letters.
The function returns whether the number of unique letters equals the total number of letters.
- For
Alpha it would return False, because a is considered to repeat A, so the number of unique letters in Alpha is 4,
and the total number of letters in Alpha is 5.
- For
Bravo it would return True, since the number of unique letters in Bravo is 5, and the total number of letters in Bravo is 5.
Source: Exercism python/isogram