From 799fff7ecdd312d70c532c29b0357347c3c88d9f Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 16 Apr 2021 14:29:58 +0200 Subject: [PATCH] Create is_good_str.py (#210) * Create is_good_str.py As discussed in #201 * Placate mypy * Update README.md --- algorithms/Python/README.md | 5 ++-- algorithms/Python/strings/is_good_str.py | 29 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 algorithms/Python/strings/is_good_str.py diff --git a/algorithms/Python/README.md b/algorithms/Python/README.md index 7e47044e..32c123e7 100644 --- a/algorithms/Python/README.md +++ b/algorithms/Python/README.md @@ -25,5 +25,6 @@ 4. [Selection Sort](sorting/selection_sort.py) ## Strings -1. [Palindrome](strings/palindrome.py) -2. [Word Count](strings/word_count.py) +1. [Is Good Str](strings/is_good_str.py) +2. [Palindrome](strings/palindrome.py) +3. [Word Count](strings/word_count.py) diff --git a/algorithms/Python/strings/is_good_str.py b/algorithms/Python/strings/is_good_str.py new file mode 100644 index 00000000..708b92b4 --- /dev/null +++ b/algorithms/Python/strings/is_good_str.py @@ -0,0 +1,29 @@ +# https://practice.geeksforgeeks.org/problems/good-or-bad-string1417/1/ + +from string import ascii_lowercase + +vowels = "aeiou" +constants = "".join(c for c in ascii_lowercase if c not in vowels) + + +def is_good_str(s: str) -> bool: + """ + >>> is_good_str("aeioup??") + True + >>> is_good_str("bcdaeiou??") + False + """ + c_or_v = "" + for char in s.lower(): + if char in constants: + c_or_v += "c" + else: + c_or_v += "v" if char in vowels else char + return not "c" * 4 in c_or_v.replace("?", "c") and ( + not "v" * 6 in c_or_v.replace("?", "v") + ) + + +if __name__ == "__main__": + print(is_good_str("aeioup??")) + print(is_good_str("bcdaeiou??"))