From 28d4d41779b198011632beb3869e873d661dd02e Mon Sep 17 00:00:00 2001 From: ANIKET PANDEY <94975684+AniketPandey22@users.noreply.github.com> Date: Wed, 16 Oct 2024 15:29:36 +0530 Subject: [PATCH 1/4] Add hollow_diamond_alphabet function for printing alphabet diamond patterns --- strings/hollow_diamond_alphabet.py | 78 ++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 strings/hollow_diamond_alphabet.py diff --git a/strings/hollow_diamond_alphabet.py b/strings/hollow_diamond_alphabet.py new file mode 100644 index 000000000000..b82676c5e146 --- /dev/null +++ b/strings/hollow_diamond_alphabet.py @@ -0,0 +1,78 @@ +def hollow_diamond_alphabet(n): + """ + Prints a hollow diamond pattern using uppercase alphabet characters. + + :param n: An integer representing the number of rows in the diamond. + :return: True if the pattern was successfully printed, False otherwise. + """ + if not isinstance(n, int): + print("Error: Input must be an integer.") + return False + + if n <= 0: + print("Error: Input must be a positive integer.") + return False + + if n % 2 == 0: + print("Error: Input must be an odd integer.") + return False + + # Calculate the number of rows for the upper half of the diamond + upper_rows = (n + 1) // 2 + + # Print the upper half of the diamond + for i in range(upper_rows): + char = chr(65 + i) # Convert number to uppercase alphabet + if i == 0: + print(" " * (upper_rows - 1) + char) + else: + print(" " * (upper_rows - i - 1) + char + " " * (2 * i - 1) + char) + + # Print the lower half of the diamond + for i in range(upper_rows - 2, -1, -1): + char = chr(65 + i) # Convert number to uppercase alphabet + if i == 0: + print(" " * (upper_rows - 1) + char) + else: + print(" " * (upper_rows - i - 1) + char + " " * (2 * i - 1) + char) + + return True + +def get_valid_input(): + """ + Prompts the user for input and validates it. + + :return: A valid positive odd integer, or None if the user chooses to quit. + """ + while True: + user_input = input("Enter the diamond size (positive odd integer) or 'q' to quit: ") + + if user_input.lower() == 'q': + return None + + try: + n = int(user_input) + if n > 0 and n % 2 != 0: + return n + elif n <= 0: + print("Error: Please enter a positive integer.") + else: + print("Error: Please enter an odd integer.") + except ValueError: + print("Error: Invalid input. Please enter a valid integer.") + +# Main program +def main(): + while True: + size = get_valid_input() + if size is None: + print("Thank you for using the Hollow Diamond Alphabet Pattern generator. Goodbye!") + break + + if hollow_diamond_alphabet(size): + print("\nDiamond pattern printed successfully!") + + print() # Add a blank line for better readability + +if __name__ == "__main__": + main() From 8896716148f9f171735f3161b20466d9450a70fd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 16 Oct 2024 10:09:26 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/hollow_diamond_alphabet.py | 37 ++++++++++++++++++------------ 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/strings/hollow_diamond_alphabet.py b/strings/hollow_diamond_alphabet.py index b82676c5e146..436441587e5b 100644 --- a/strings/hollow_diamond_alphabet.py +++ b/strings/hollow_diamond_alphabet.py @@ -1,25 +1,25 @@ def hollow_diamond_alphabet(n): """ Prints a hollow diamond pattern using uppercase alphabet characters. - + :param n: An integer representing the number of rows in the diamond. :return: True if the pattern was successfully printed, False otherwise. """ if not isinstance(n, int): print("Error: Input must be an integer.") return False - + if n <= 0: print("Error: Input must be a positive integer.") return False - + if n % 2 == 0: print("Error: Input must be an odd integer.") return False - + # Calculate the number of rows for the upper half of the diamond upper_rows = (n + 1) // 2 - + # Print the upper half of the diamond for i in range(upper_rows): char = chr(65 + i) # Convert number to uppercase alphabet @@ -27,7 +27,7 @@ def hollow_diamond_alphabet(n): print(" " * (upper_rows - 1) + char) else: print(" " * (upper_rows - i - 1) + char + " " * (2 * i - 1) + char) - + # Print the lower half of the diamond for i in range(upper_rows - 2, -1, -1): char = chr(65 + i) # Convert number to uppercase alphabet @@ -35,21 +35,24 @@ def hollow_diamond_alphabet(n): print(" " * (upper_rows - 1) + char) else: print(" " * (upper_rows - i - 1) + char + " " * (2 * i - 1) + char) - + return True + def get_valid_input(): """ Prompts the user for input and validates it. - + :return: A valid positive odd integer, or None if the user chooses to quit. """ while True: - user_input = input("Enter the diamond size (positive odd integer) or 'q' to quit: ") - - if user_input.lower() == 'q': + user_input = input( + "Enter the diamond size (positive odd integer) or 'q' to quit: " + ) + + if user_input.lower() == "q": return None - + try: n = int(user_input) if n > 0 and n % 2 != 0: @@ -61,18 +64,22 @@ def get_valid_input(): except ValueError: print("Error: Invalid input. Please enter a valid integer.") + # Main program def main(): while True: size = get_valid_input() if size is None: - print("Thank you for using the Hollow Diamond Alphabet Pattern generator. Goodbye!") + print( + "Thank you for using the Hollow Diamond Alphabet Pattern generator. Goodbye!" + ) break - + if hollow_diamond_alphabet(size): print("\nDiamond pattern printed successfully!") - + print() # Add a blank line for better readability + if __name__ == "__main__": main() From d5e0b74cdfa62177bf5f8d81e1cb31079ead7e30 Mon Sep 17 00:00:00 2001 From: ANIKET PANDEY <94975684+AniketPandey22@users.noreply.github.com> Date: Wed, 16 Oct 2024 15:47:37 +0530 Subject: [PATCH 3/4] Update hollow_diamond_alphabet.py --- strings/hollow_diamond_alphabet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/strings/hollow_diamond_alphabet.py b/strings/hollow_diamond_alphabet.py index 436441587e5b..0bad2d622af8 100644 --- a/strings/hollow_diamond_alphabet.py +++ b/strings/hollow_diamond_alphabet.py @@ -71,7 +71,7 @@ def main(): size = get_valid_input() if size is None: print( - "Thank you for using the Hollow Diamond Alphabet Pattern generator. Goodbye!" + "Thank you!" ) break From 759ab7d3426a34cfa6bd849e4a9e983e91be0f76 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 16 Oct 2024 10:17:59 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/hollow_diamond_alphabet.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/strings/hollow_diamond_alphabet.py b/strings/hollow_diamond_alphabet.py index 0bad2d622af8..c5b27cc3f926 100644 --- a/strings/hollow_diamond_alphabet.py +++ b/strings/hollow_diamond_alphabet.py @@ -70,9 +70,7 @@ def main(): while True: size = get_valid_input() if size is None: - print( - "Thank you!" - ) + print("Thank you!") break if hollow_diamond_alphabet(size):