Add DocTests to magicdiamondpattern.py (#10135)

* magicdiamondpattern doctest

* remove start part

---------

Co-authored-by: Harsha Kottapalli <skottapalli@microsoft.com>
This commit is contained in:
Sai Harsha Kottapalli 2023-10-09 17:46:43 +05:30 committed by GitHub
parent 12e8e9ca87
commit 876087be99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,52 +4,76 @@
# Function to print upper half of diamond (pyramid) # Function to print upper half of diamond (pyramid)
def floyd(n): def floyd(n):
""" """
Parameters: Print the upper half of a diamond pattern with '*' characters.
n : size of pattern
Args:
n (int): Size of the pattern.
Examples:
>>> floyd(3)
' * \\n * * \\n* * * \\n'
>>> floyd(5)
' * \\n * * \\n * * * \\n * * * * \\n* * * * * \\n'
""" """
result = ""
for i in range(n): for i in range(n):
for _ in range(n - i - 1): # printing spaces for _ in range(n - i - 1): # printing spaces
print(" ", end="") result += " "
for _ in range(i + 1): # printing stars for _ in range(i + 1): # printing stars
print("* ", end="") result += "* "
print() result += "\n"
return result
# Function to print lower half of diamond (pyramid) # Function to print lower half of diamond (pyramid)
def reverse_floyd(n): def reverse_floyd(n):
""" """
Parameters: Print the lower half of a diamond pattern with '*' characters.
n : size of pattern
Args:
n (int): Size of the pattern.
Examples:
>>> reverse_floyd(3)
'* * * \\n * * \\n * \\n '
>>> reverse_floyd(5)
'* * * * * \\n * * * * \\n * * * \\n * * \\n * \\n '
""" """
result = ""
for i in range(n, 0, -1): for i in range(n, 0, -1):
for _ in range(i, 0, -1): # printing stars for _ in range(i, 0, -1): # printing stars
print("* ", end="") result += "* "
print() result += "\n"
for _ in range(n - i + 1, 0, -1): # printing spaces for _ in range(n - i + 1, 0, -1): # printing spaces
print(" ", end="") result += " "
return result
# Function to print complete diamond pattern of "*" # Function to print complete diamond pattern of "*"
def pretty_print(n): def pretty_print(n):
""" """
Parameters: Print a complete diamond pattern with '*' characters.
n : size of pattern
Args:
n (int): Size of the pattern.
Examples:
>>> pretty_print(0)
' ... .... nothing printing :('
>>> pretty_print(3)
' * \\n * * \\n* * * \\n* * * \\n * * \\n * \\n '
""" """
if n <= 0: if n <= 0:
print(" ... .... nothing printing :(") return " ... .... nothing printing :("
return upper_half = floyd(n) # upper half
floyd(n) # upper half lower_half = reverse_floyd(n) # lower half
reverse_floyd(n) # lower half return upper_half + lower_half
if __name__ == "__main__": if __name__ == "__main__":
print(r"| /\ | |- | |- |--| |\ /| |-") import doctest
print(r"|/ \| |- |_ |_ |__| | \/ | |_")
K = 1
while K:
user_number = int(input("enter the number and , and see the magic : "))
print()
pretty_print(user_number)
K = int(input("press 0 to exit... and 1 to continue..."))
print("Good Bye...") doctest.testmod()