From d54a45a6814ba322bcb451d398e6dc1cea600077 Mon Sep 17 00:00:00 2001 From: M3tal M0nk3y Date: Mon, 24 Oct 2022 21:52:17 -0400 Subject: [PATCH 1/8] Added function that checks if a string is an isogram. --- strings/check_isogram.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 strings/check_isogram.py diff --git a/strings/check_isogram.py b/strings/check_isogram.py new file mode 100644 index 000000000000..4b8272c7248f --- /dev/null +++ b/strings/check_isogram.py @@ -0,0 +1,25 @@ +""" +An isogram is a word in which no letter is repeated. +Examples of isograms are uncopyrightable and ambidextrously. +""" + +def check_isogram(string: str) -> bool: + """ + >>> check_isogram('uncopyrightable') + True + >>> check_isogram('allowance') + False + """ + letters = sorted(string) + + for idx, letter in enumerate(letters): + if letter == letters[idx - 1]: + return False + + return True + +if __name__ == "__main__": + input_str = input("Enter a string ").strip().lower() + + isogram = check_isogram(input_str) + print(f"{input_str} is {'an' if isogram else 'not an'} isogram.") \ No newline at end of file From 609a793a026122216fb219dbabf4f4d87ef7b25d Mon Sep 17 00:00:00 2001 From: M3tal M0nk3y Date: Mon, 24 Oct 2022 22:06:21 -0400 Subject: [PATCH 2/8] Added wiki reference and fixed comments. --- strings/check_isogram.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/strings/check_isogram.py b/strings/check_isogram.py index 4b8272c7248f..902edce4bd33 100644 --- a/strings/check_isogram.py +++ b/strings/check_isogram.py @@ -1,10 +1,12 @@ """ -An isogram is a word in which no letter is repeated. -Examples of isograms are uncopyrightable and ambidextrously. +wiki: https://en.wikipedia.org/wiki/Heterogram_(literature)#Isograms """ + def check_isogram(string: str) -> bool: """ + An isogram is a word in which no letter is repeated. + Examples of isograms are uncopyrightable and ambidextrously. >>> check_isogram('uncopyrightable') True >>> check_isogram('allowance') From e52856035bb7d4f80cef5e92c455ed66f0a73f87 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 25 Oct 2022 02:08:20 +0000 Subject: [PATCH 3/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/check_isogram.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/strings/check_isogram.py b/strings/check_isogram.py index 902edce4bd33..b36e5b5a9456 100644 --- a/strings/check_isogram.py +++ b/strings/check_isogram.py @@ -5,7 +5,7 @@ def check_isogram(string: str) -> bool: """ - An isogram is a word in which no letter is repeated. + An isogram is a word in which no letter is repeated. Examples of isograms are uncopyrightable and ambidextrously. >>> check_isogram('uncopyrightable') True @@ -20,8 +20,9 @@ def check_isogram(string: str) -> bool: return True + if __name__ == "__main__": input_str = input("Enter a string ").strip().lower() isogram = check_isogram(input_str) - print(f"{input_str} is {'an' if isogram else 'not an'} isogram.") \ No newline at end of file + print(f"{input_str} is {'an' if isogram else 'not an'} isogram.") From fe25aeaca82162c24e15dd87cd6d96923face808 Mon Sep 17 00:00:00 2001 From: M3tal M0nk3y Date: Tue, 25 Oct 2022 21:53:05 -0400 Subject: [PATCH 4/8] Made function name more self-documenting. Raise ValueError if string contains 1 or more digits. Renamed file. Lowercase string inside function. --- strings/check_isogram.py | 27 --------------------------- strings/is_isogram.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 27 deletions(-) delete mode 100644 strings/check_isogram.py create mode 100644 strings/is_isogram.py diff --git a/strings/check_isogram.py b/strings/check_isogram.py deleted file mode 100644 index 902edce4bd33..000000000000 --- a/strings/check_isogram.py +++ /dev/null @@ -1,27 +0,0 @@ -""" -wiki: https://en.wikipedia.org/wiki/Heterogram_(literature)#Isograms -""" - - -def check_isogram(string: str) -> bool: - """ - An isogram is a word in which no letter is repeated. - Examples of isograms are uncopyrightable and ambidextrously. - >>> check_isogram('uncopyrightable') - True - >>> check_isogram('allowance') - False - """ - letters = sorted(string) - - for idx, letter in enumerate(letters): - if letter == letters[idx - 1]: - return False - - return True - -if __name__ == "__main__": - input_str = input("Enter a string ").strip().lower() - - isogram = check_isogram(input_str) - print(f"{input_str} is {'an' if isogram else 'not an'} isogram.") \ No newline at end of file diff --git a/strings/is_isogram.py b/strings/is_isogram.py new file mode 100644 index 000000000000..3fa1c5cbb73b --- /dev/null +++ b/strings/is_isogram.py @@ -0,0 +1,28 @@ +""" +wiki: https://en.wikipedia.org/wiki/Heterogram_(literature)#Isograms +""" +import re + + +def is_isogram(string: str) -> bool: + """ + An isogram is a word in which no letter is repeated. + Examples of isograms are uncopyrightable and ambidextrously. + >>> is_isogram('Uncopyrightable') + True + >>> is_isogram('allowance') + False + >>> is_isogram('copy1') + ValueError: String must only contain alphabetic characters. + """ + if bool(re.search(r'\d', string)): + raise ValueError("String must only contain alphabetic characters.") + + letters = sorted(string.lower()) + return len(letters) == len(set(letters)) + +if __name__ == "__main__": + input_str = input("Enter a string ").strip() + + isogram = is_isogram(input_str) + print(f"{input_str} is {'an' if isogram else 'not an'} isogram.") \ No newline at end of file From a1828c64cded8fe75eee2b5cf7e939932beec846 Mon Sep 17 00:00:00 2001 From: M3tal M0nk3y Date: Tue, 25 Oct 2022 21:56:19 -0400 Subject: [PATCH 5/8] Removed check_isogram.py (file renamed). --- strings/check_isogram.py | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 strings/check_isogram.py diff --git a/strings/check_isogram.py b/strings/check_isogram.py deleted file mode 100644 index b36e5b5a9456..000000000000 --- a/strings/check_isogram.py +++ /dev/null @@ -1,28 +0,0 @@ -""" -wiki: https://en.wikipedia.org/wiki/Heterogram_(literature)#Isograms -""" - - -def check_isogram(string: str) -> bool: - """ - An isogram is a word in which no letter is repeated. - Examples of isograms are uncopyrightable and ambidextrously. - >>> check_isogram('uncopyrightable') - True - >>> check_isogram('allowance') - False - """ - letters = sorted(string) - - for idx, letter in enumerate(letters): - if letter == letters[idx - 1]: - return False - - return True - - -if __name__ == "__main__": - input_str = input("Enter a string ").strip().lower() - - isogram = check_isogram(input_str) - print(f"{input_str} is {'an' if isogram else 'not an'} isogram.") From 7dba5f1151b6b6535781e0282ec7b656bbbdea7b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 26 Oct 2022 01:56:23 +0000 Subject: [PATCH 6/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/is_isogram.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/strings/is_isogram.py b/strings/is_isogram.py index 214b3e27f3c8..9cb309d382bd 100644 --- a/strings/is_isogram.py +++ b/strings/is_isogram.py @@ -6,7 +6,7 @@ def is_isogram(string: str) -> bool: """ - An isogram is a word in which no letter is repeated. + An isogram is a word in which no letter is repeated. Examples of isograms are uncopyrightable and ambidextrously. >>> is_isogram('Uncopyrightable') True @@ -15,7 +15,7 @@ def is_isogram(string: str) -> bool: >>> is_isogram('copy1') ValueError: String must only contain alphabetic characters. """ - if bool(re.search(r'\d', string)): + if bool(re.search(r"\d", string)): raise ValueError("String must only contain alphabetic characters.") letters = sorted(string.lower()) From a2a4a00cc19ed83c5724e210121bdb71565f5765 Mon Sep 17 00:00:00 2001 From: M3tal M0nk3y Date: Tue, 25 Oct 2022 22:05:09 -0400 Subject: [PATCH 7/8] Fixed test failure. --- strings/is_isogram.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/strings/is_isogram.py b/strings/is_isogram.py index 214b3e27f3c8..9a503a0e183b 100644 --- a/strings/is_isogram.py +++ b/strings/is_isogram.py @@ -1,7 +1,6 @@ """ wiki: https://en.wikipedia.org/wiki/Heterogram_(literature)#Isograms """ -import re def is_isogram(string: str) -> bool: @@ -13,8 +12,12 @@ def is_isogram(string: str) -> bool: >>> is_isogram('allowance') False >>> is_isogram('copy1') + Traceback (most recent call last): + ... ValueError: String must only contain alphabetic characters. """ + import re + if bool(re.search(r'\d', string)): raise ValueError("String must only contain alphabetic characters.") From 21077499c31cd9f71415046eedcdda90ba1bbd44 Mon Sep 17 00:00:00 2001 From: M3tal M0nk3y Date: Tue, 25 Oct 2022 23:24:29 -0400 Subject: [PATCH 8/8] Raise ValueError when string has non-alpha characters. Removed import. --- strings/is_isogram.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/strings/is_isogram.py b/strings/is_isogram.py index 43c56498eed2..a9d9acc8138e 100644 --- a/strings/is_isogram.py +++ b/strings/is_isogram.py @@ -16,9 +16,7 @@ def is_isogram(string: str) -> bool: ... ValueError: String must only contain alphabetic characters. """ - import re - - if bool(re.search(r"\d", string)): + if not all(x.isalpha() for x in string): raise ValueError("String must only contain alphabetic characters.") letters = sorted(string.lower())