-
-
Notifications
You must be signed in to change notification settings - Fork 46.1k
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
Changes from all commits
28d4d41
8896716
d5e0b74
759ab7d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
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(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Please provide return type hint for the function: |
||
""" | ||
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(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Please provide return type hint for the function: |
||
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() |
There was a problem hiding this comment.
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 functionhollow_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