MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / butterfly_pattern

Function butterfly_pattern

graphics/butterfly_pattern.py:1–41  ·  view source on GitHub ↗

Creates a butterfly pattern of size n and returns it as a string. >>> print(butterfly_pattern(3)) * * ** ** ***** ** ** * * >>> print(butterfly_pattern(5)) * * ** ** *** *** **** **** ********* **** **** *** *** **

(n: int)

Source from the content-addressed store, hash-verified

1def butterfly_pattern(n: int) -> str:
2 """
3 Creates a butterfly pattern of size n and returns it as a string.
4
5 >>> print(butterfly_pattern(3))
6 * *
7 ** **
8 *****
9 ** **
10 * *
11 >>> print(butterfly_pattern(5))
12 * *
13 ** **
14 *** ***
15 **** ****
16 *********
17 **** ****
18 *** ***
19 ** **
20 * *
21 """
22 result = []
23
24 # Upper part
25 for i in range(1, n):
26 left_stars = "*" * i
27 spaces = " " * (2 * (n - i) - 1)
28 right_stars = "*" * i
29 result.append(left_stars + spaces + right_stars)
30
31 # Middle part
32 result.append("*" * (2 * n - 1))
33
34 # Lower part
35 for i in range(n - 1, 0, -1):
36 left_stars = "*" * i
37 spaces = " " * (2 * (n - i) - 1)
38 right_stars = "*" * i
39 result.append(left_stars + spaces + right_stars)
40
41 return "\n".join(result)
42
43
44if __name__ == "__main__":

Callers 1

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected