Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5b4b242

Browse files
author
Medha
committedDec 23, 2024·
added butterfy_pattern under strings folder
1 parent dd311fb commit 5b4b242

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
 

‎strings/butterfly_pattern.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
def print_butterfly(n: int) -> None:
2+
"""
3+
Prints a butterfly pattern using the character '*' based on the input size.
4+
5+
The butterfly pattern has a symmetrical structure with an upper and lower half.
6+
7+
:param n: The size of the butterfly (side length).
8+
:return: None
9+
10+
Example:
11+
>>> print_butterfly(5)
12+
* *
13+
** **
14+
*** ***
15+
**** ****
16+
*********
17+
**** ****
18+
*** ***
19+
** **
20+
* *
21+
"""
22+
# Upper half of the butterfly
23+
for i in range(1, n + 1):
24+
print("*" * i, end="")
25+
print(" " * (2 * (n - i)), end="")
26+
print("*" * i)
27+
28+
# Lower half of the butterfly
29+
for i in range(n, 0, -1):
30+
print("*" * i, end="")
31+
print(" " * (2 * (n - i)), end="")
32+
print("*" * i)
33+
34+
35+
# Ask the user for input and print the butterfly
36+
n = int(input("Enter the value of n: "))
37+
print_butterfly(n)

0 commit comments

Comments
 (0)
Please sign in to comment.