Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hollow_diamond_alphabet function for printing alphabet diamond patterns. #12116

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions strings/hollow_diamond_alphabet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
def hollow_diamond_alphabet(n):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file strings/hollow_diamond_alphabet.py, please provide doctest for the function hollow_diamond_alphabet

Please provide return type hint for the function: hollow_diamond_alphabet. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: n

Please provide type hint for the parameter: 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():

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file strings/hollow_diamond_alphabet.py, please provide doctest for the function get_valid_input

Please provide return type hint for the function: get_valid_input. If the function does not return a value, please provide the type hint as: def function() -> None:

"""
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():

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file strings/hollow_diamond_alphabet.py, please provide doctest for the function main

Please provide return type hint for the function: main. If the function does not return a value, please provide the type hint as: def function() -> None:

while True:
size = get_valid_input()
if size is None:
print("Thank you!")
break

if hollow_diamond_alphabet(size):
print("\nDiamond pattern printed successfully!")

print() # Add a blank line for better readability


if __name__ == "__main__":
main()