# Python program for generating diamond pattern in Python 3.7+# Function to print upper half of diamond (pyramid)def floyd(n):"""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 spacesresult += " "for _ in range(i + 1): # printing starsresult += "* "result += "\n"return result# Function to print lower half of diamond (pyramid)def reverse_floyd(n):"""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 starsresult += "* "result += "\n"for _ in range(n - i + 1, 0, -1): # printing spacesresult += " "return result# Function to print complete diamond pattern of "*"def pretty_print(n):"""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:return " ... .... nothing printing :("upper_half = floyd(n) # upper halflower_half = reverse_floyd(n) # lower halfreturn upper_half + lower_halfif __name__ == "__main__":import doctestdoctest.testmod()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. Open source ecosystem
2. Collaboration, People, Software
3. Evaluation model