import re
def is_isogram(phrase):
scrubbed = "".join(re.findall("[a-zA-Z]", phrase)).lower()
return len(set(scrubbed)) == len(scrubbed)
For this approach, regular expression pattern, also known as a regex, is used to filter 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 areathroughzorAthroughZ. This essentially matches any characters which are in the English alphabet. The pattern is passed to thefindall()method to return a list of matched characters. - The result of
findall()is passed as an argument to thejoin()method, which is called on an empty string. This makes a string out of the list of matched characters. - The output of
join()is then chained as the input forlower(). All of the letters are lowercased so that letters of different cases will become the same letter for comparison purposes, sinceAandaare considered to be the same letter. When the filtering and lowercasing is done, the scrubbed variable will be a string having all alphabetic letters lowercased. - A
setis constructed from the scrubbed string and itslenis compared with thelenof the the scrubbed string. Since asetholds 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
Alphait would returnFalse, becauseais considered to repeatA, so the number of unique letters inAlphais4, and the total number of letters inAlphais5. - For
Bravoit would returnTrue, since the number of unique letters inBravois5, and the total number of letters inBravois5.
4th Sep 2024
·
Found it useful?