2024.4.19(周五)

def sierpinski_triangle(n):
if n == 1:
return " /\\", "/--\\"

prev_triangle = sierpinski_triangle(n - 1)
size = len(prev_triangle)
new_triangle = []

# Add the top part of the triangle
for row in prev_triangle:
new_triangle.append(" " * size + row + " " * size)

# Add the bottom part of the triangle
for row in prev_triangle:
new_triangle.append(row + " " + row)

return new_triangle


def print_sierpinski_triangle(n):
triangle = sierpinski_triangle(n)
for row in triangle:
print(row)


# Test cases
print("Test case 1: 3")
print_sierpinski_triangle(3)
print("\nTest case 2: 2")
print_sierpinski_triangle(2)
print("\nTest case 3: 1")
print_sierpinski_triangle(1)



posted @ 2024-04-24 20:26  记得关月亮  阅读(2)  评论(0编辑  收藏  举报