File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change 1+ def 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+
44+ if __name__ == "__main__" :
45+ n = int (input ("Enter the size of the butterfly pattern: " ))
46+ print (butterfly_pattern (n ))
You can’t perform that action at this time.
0 commit comments