2020-03-04 12:40:28 +00:00
|
|
|
# Python program for generating diamond pattern in Python 3.7+
|
2019-10-18 22:02:32 +00:00
|
|
|
|
2020-05-22 06:10:11 +00:00
|
|
|
|
2019-10-18 22:02:32 +00:00
|
|
|
# Function to print upper half of diamond (pyramid)
|
|
|
|
def floyd(n):
|
2019-10-22 17:13:48 +00:00
|
|
|
"""
|
2023-10-09 12:16:43 +00:00
|
|
|
Print the upper half of a diamond pattern with '*' characters.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
n (int): Size of the pattern.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
>>> floyd(3)
|
|
|
|
' * \\n * * \\n* * * \\n'
|
|
|
|
|
|
|
|
>>> floyd(5)
|
|
|
|
' * \\n * * \\n * * * \\n * * * * \\n* * * * * \\n'
|
2020-09-10 08:31:26 +00:00
|
|
|
"""
|
2023-10-09 12:16:43 +00:00
|
|
|
result = ""
|
2023-08-29 13:18:10 +00:00
|
|
|
for i in range(n):
|
|
|
|
for _ in range(n - i - 1): # printing spaces
|
2023-10-09 12:16:43 +00:00
|
|
|
result += " "
|
2023-08-29 13:18:10 +00:00
|
|
|
for _ in range(i + 1): # printing stars
|
2023-10-09 12:16:43 +00:00
|
|
|
result += "* "
|
|
|
|
result += "\n"
|
|
|
|
return result
|
2019-10-18 22:02:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Function to print lower half of diamond (pyramid)
|
|
|
|
def reverse_floyd(n):
|
2019-10-22 17:13:48 +00:00
|
|
|
"""
|
2023-10-09 12:16:43 +00:00
|
|
|
Print the lower half of a diamond pattern with '*' characters.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
n (int): Size of the pattern.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
>>> reverse_floyd(3)
|
|
|
|
'* * * \\n * * \\n * \\n '
|
|
|
|
|
|
|
|
>>> reverse_floyd(5)
|
|
|
|
'* * * * * \\n * * * * \\n * * * \\n * * \\n * \\n '
|
2020-09-10 08:31:26 +00:00
|
|
|
"""
|
2023-10-09 12:16:43 +00:00
|
|
|
result = ""
|
2019-10-22 17:13:48 +00:00
|
|
|
for i in range(n, 0, -1):
|
2022-10-13 16:03:06 +00:00
|
|
|
for _ in range(i, 0, -1): # printing stars
|
2023-10-09 12:16:43 +00:00
|
|
|
result += "* "
|
|
|
|
result += "\n"
|
2022-10-13 16:03:06 +00:00
|
|
|
for _ in range(n - i + 1, 0, -1): # printing spaces
|
2023-10-09 12:16:43 +00:00
|
|
|
result += " "
|
|
|
|
return result
|
2019-10-22 17:13:48 +00:00
|
|
|
|
2019-10-18 22:02:32 +00:00
|
|
|
|
|
|
|
# Function to print complete diamond pattern of "*"
|
|
|
|
def pretty_print(n):
|
2019-10-22 17:13:48 +00:00
|
|
|
"""
|
2023-10-09 12:16:43 +00:00
|
|
|
Print a complete diamond pattern with '*' characters.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
n (int): Size of the pattern.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
>>> pretty_print(0)
|
|
|
|
' ... .... nothing printing :('
|
|
|
|
|
|
|
|
>>> pretty_print(3)
|
|
|
|
' * \\n * * \\n* * * \\n* * * \\n * * \\n * \\n '
|
2020-09-10 08:31:26 +00:00
|
|
|
"""
|
2019-10-22 17:13:48 +00:00
|
|
|
if n <= 0:
|
2023-10-09 12:16:43 +00:00
|
|
|
return " ... .... nothing printing :("
|
|
|
|
upper_half = floyd(n) # upper half
|
|
|
|
lower_half = reverse_floyd(n) # lower half
|
|
|
|
return upper_half + lower_half
|
2019-10-18 22:02:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-10-09 12:16:43 +00:00
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|