Skip to content

Add hollow_diamond_alphabet function for printing alphabet diamond patterns. #12116

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

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
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
Next Next commit
Add hollow_diamond_alphabet function for printing alphabet diamond pa…
…tterns
  • Loading branch information
AniketPandey22 authored Oct 16, 2024
commit 28d4d41779b198011632beb3869e873d661dd02e
78 changes: 78 additions & 0 deletions strings/hollow_diamond_alphabet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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.

Check failure on line 4 in strings/hollow_diamond_alphabet.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

strings/hollow_diamond_alphabet.py:4:1: W293 Blank line contains whitespace
: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

Check failure on line 11 in strings/hollow_diamond_alphabet.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

strings/hollow_diamond_alphabet.py:11:1: W293 Blank line contains whitespace
if n <= 0:
print("Error: Input must be a positive integer.")
return False

Check failure on line 15 in strings/hollow_diamond_alphabet.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

strings/hollow_diamond_alphabet.py:15:1: W293 Blank line contains whitespace
if n % 2 == 0:
print("Error: Input must be an odd integer.")
return False

Check failure on line 19 in strings/hollow_diamond_alphabet.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

strings/hollow_diamond_alphabet.py:19:1: W293 Blank line contains whitespace
# Calculate the number of rows for the upper half of the diamond
upper_rows = (n + 1) // 2

Check failure on line 22 in strings/hollow_diamond_alphabet.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

strings/hollow_diamond_alphabet.py:22:1: W293 Blank line contains whitespace
# 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)

Check failure on line 30 in strings/hollow_diamond_alphabet.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

strings/hollow_diamond_alphabet.py:30:1: W293 Blank line contains whitespace
# 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)

Check failure on line 38 in strings/hollow_diamond_alphabet.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

strings/hollow_diamond_alphabet.py:38:1: W293 Blank line contains whitespace
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.

Check failure on line 44 in strings/hollow_diamond_alphabet.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

strings/hollow_diamond_alphabet.py:44:1: W293 Blank line contains whitespace
: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: ")

Check failure on line 48 in strings/hollow_diamond_alphabet.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

strings/hollow_diamond_alphabet.py:48:89: E501 Line too long (92 > 88)

Check failure on line 49 in strings/hollow_diamond_alphabet.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

strings/hollow_diamond_alphabet.py:49:1: W293 Blank line contains whitespace
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 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()