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 thefor
loop. - The result of each iteration is
ltr.lower()
, which is thelower
cased letter being iterated. All of the letters are lowercased so that letters of different cases will become the same letter for comparison purposes, sinceA
anda
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()
returnsTrue
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 itslen
is compared with thelen
of the the scrubbed list. Since aset
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 returnFalse
, becausea
is considered to repeatA
, so the number of unique letters inAlpha
is4
, and the total number of letters inAlpha
is5
. - For
Bravo
it would returnTrue
, since the number of unique letters inBravo
is5
, and the total number of letters inBravo
is5
.
20th Nov 2024
·
Found it useful?