From 99e4588e3d2bb7a11d9b2d7f7f9248321b3ec6dd Mon Sep 17 00:00:00 2001 From: Harsha Kottapalli Date: Sun, 8 Oct 2023 23:17:12 +0530 Subject: [PATCH 1/2] magicdiamondpattern doctest --- other/magicdiamondpattern.py | 69 +++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 17 deletions(-) diff --git a/other/magicdiamondpattern.py b/other/magicdiamondpattern.py index 89b973bb41e8..400fd1914290 100644 --- a/other/magicdiamondpattern.py +++ b/other/magicdiamondpattern.py @@ -4,42 +4,73 @@ # Function to print upper half of diamond (pyramid) def floyd(n): """ - Parameters: - n : size of pattern + 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' """ + result = "" for i in range(n): for _ in range(n - i - 1): # printing spaces - print(" ", end="") + result += " " for _ in range(i + 1): # printing stars - print("* ", end="") - print() + result += "* " + result += "\n" + return result # Function to print lower half of diamond (pyramid) def reverse_floyd(n): """ - Parameters: - n : size of pattern + 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 ' """ + result = "" for i in range(n, 0, -1): for _ in range(i, 0, -1): # printing stars - print("* ", end="") - print() + result += "* " + result += "\n" for _ in range(n - i + 1, 0, -1): # printing spaces - print(" ", end="") + result += " " + return result # Function to print complete diamond pattern of "*" def pretty_print(n): """ - Parameters: - n : size of pattern + 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 ' """ if n <= 0: - print(" ... .... nothing printing :(") - return - floyd(n) # upper half - reverse_floyd(n) # lower half + return " ... .... nothing printing :(" + upper_half = floyd(n) # upper half + lower_half = reverse_floyd(n) # lower half + return upper_half + lower_half if __name__ == "__main__": @@ -49,7 +80,11 @@ def pretty_print(n): while K: user_number = int(input("enter the number and , and see the magic : ")) print() - pretty_print(user_number) + print(pretty_print(user_number)) K = int(input("press 0 to exit... and 1 to continue...")) print("Good Bye...") + + import doctest + + doctest.testmod() From f1d756ae17ab8ee8c9e6c90132a8e5d4829a22f1 Mon Sep 17 00:00:00 2001 From: Harsha Kottapalli Date: Sun, 8 Oct 2023 23:26:27 +0530 Subject: [PATCH 2/2] remove start part --- other/magicdiamondpattern.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/other/magicdiamondpattern.py b/other/magicdiamondpattern.py index 400fd1914290..58889280ab17 100644 --- a/other/magicdiamondpattern.py +++ b/other/magicdiamondpattern.py @@ -74,17 +74,6 @@ def pretty_print(n): if __name__ == "__main__": - print(r"| /\ | |- | |- |--| |\ /| |-") - print(r"|/ \| |- |_ |_ |__| | \/ | |_") - K = 1 - while K: - user_number = int(input("enter the number and , and see the magic : ")) - print() - print(pretty_print(user_number)) - K = int(input("press 0 to exit... and 1 to continue...")) - - print("Good Bye...") - import doctest doctest.testmod()