File tree 2 files changed +31
-9
lines changed
2 files changed +31
-9
lines changed Original file line number Diff line number Diff line change 1
- def butterfly_pattern (n ):
1
+ def butterfly_pattern (n : int ) -> str :
2
+ """
3
+ Creates a butterfly pattern of size n and returns it as a string.
4
+ """
5
+ result = []
6
+
2
7
# Upper part
3
8
for i in range (1 , n + 1 ):
4
- print ("*" * i , end = "" )
5
- print (" " * (2 * (n - i )), end = "" )
6
- print ("*" * i )
9
+ left_stars = "*" * i
10
+ spaces = " " * (2 * (n - i + 2 ))
11
+ right_stars = "*" * i
12
+ result .append (left_stars + spaces + right_stars )
7
13
8
14
# Lower part
9
15
for i in range (n - 1 , 0 , - 1 ):
10
- print ("*" * i , end = "" )
11
- print (" " * (2 * (n - i )), end = "" )
12
- print ("*" * i )
16
+ left_stars = "*" * i
17
+ spaces = " " * (2 * (n - i + 2 ))
18
+ right_stars = "*" * i
19
+ result .append (left_stars + spaces + right_stars )
20
+
21
+ return "\n " .join (result )
22
+
13
23
24
+ if __name__ == "__main__" :
25
+ n = int (input ("Enter the size of the butterfly pattern: " ))
26
+ print (butterfly_pattern (n ))
14
27
15
- n = int (input ("Enter the size: " ))
16
- butterfly_pattern (n )
Original file line number Diff line number Diff line change
1
+ from graphics .butterfly_pattern import butterfly_pattern
2
+
3
+ def test_butterfly_pattern ():
4
+ expected_output = (
5
+ "* *\n "
6
+ "** **\n "
7
+ "*** ***\n "
8
+ "** **\n "
9
+ "* *"
10
+ )
11
+ assert butterfly_pattern (3 ) == expected_output
You can’t perform that action at this time.
0 commit comments