Skip to content

Commit 68f6e9a

Browse files
M3talM0nk3ypre-commit-ci[bot]
andauthoredOct 26, 2022
Added function that checks if a string is an isogram (TheAlgorithms#7608)
* Added function that checks if a string is an isogram. * Added wiki reference and fixed comments. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Made function name more self-documenting. Raise ValueError if string contains 1 or more digits. Renamed file. Lowercase string inside function. * Removed check_isogram.py (file renamed). * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fixed test failure. * Raise ValueError when string has non-alpha characters. Removed import. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 505c5e2 commit 68f6e9a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
 

‎strings/is_isogram.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
wiki: https://en.wikipedia.org/wiki/Heterogram_(literature)#Isograms
3+
"""
4+
5+
6+
def is_isogram(string: str) -> bool:
7+
"""
8+
An isogram is a word in which no letter is repeated.
9+
Examples of isograms are uncopyrightable and ambidextrously.
10+
>>> is_isogram('Uncopyrightable')
11+
True
12+
>>> is_isogram('allowance')
13+
False
14+
>>> is_isogram('copy1')
15+
Traceback (most recent call last):
16+
...
17+
ValueError: String must only contain alphabetic characters.
18+
"""
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())
23+
return len(letters) == len(set(letters))
24+
25+
26+
if __name__ == "__main__":
27+
input_str = input("Enter a string ").strip()
28+
29+
isogram = is_isogram(input_str)
30+
print(f"{input_str} is {'an' if isogram else 'not an'} isogram.")

0 commit comments

Comments
 (0)
Please sign in to comment.