Skip to content

Commit 8a6d45a

Browse files
authored
Improve isogram check by ignoring non-alphabetic characters
Updated docstring to clarify isogram definition and added example with non-alphabetic characters. Modified letter extraction to ignore non-alphabetic characters.
1 parent 678dedb commit 8a6d45a

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

strings/is_isogram.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,23 @@
55

66
def is_isogram(string: str) -> bool:
77
"""
8-
An isogram is a word in which no letter is repeated.
9-
Examples of isograms are uncopyrightable and ambidextrously.
8+
An isogram is a word or phrase in which no letter is repeated.
9+
Non-alphabetic characters are ignored.
10+
1011
>>> is_isogram('Uncopyrightable')
1112
True
1213
>>> is_isogram('allowance')
1314
False
15+
>>> is_isogram('six-year-old')
16+
True
1417
>>> is_isogram('copy1')
15-
Traceback (most recent call last):
16-
...
17-
ValueError: String must only contain alphabetic characters.
18+
True
1819
"""
19-
if not all(x.isalpha() for x in string):
20-
raise ValueError("String must only contain alphabetic characters.")
21-
22-
letters = sorted(string.lower())
20+
letters = [char.lower() for char in string if char.isalpha()]
2321
return len(letters) == len(set(letters))
2422

2523

2624
if __name__ == "__main__":
2725
input_str = input("Enter a string ").strip()
28-
2926
isogram = is_isogram(input_str)
3027
print(f"{input_str} is {'an' if isogram else 'not an'} isogram.")

0 commit comments

Comments
 (0)