File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed
Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ patterns/pyramid_patterns.py
3+
4+ Pattern Printing Examples:
5+ - Pyramid
6+ - Inverted Pyramid
7+ - Diamond
8+
9+ Demonstrates loops, functions, and conditionals for beginners.
10+
11+ Example:
12+ >>> pyramid(3)
13+ *
14+ ***
15+ *****
16+
17+ >>> inverted_pyramid(3)
18+ *****
19+ ***
20+ *
21+
22+ >>> diamond(3)
23+ *
24+ ***
25+ *****
26+ ***
27+ *
28+ """
29+
30+ # No import needed for None type annotation
31+
32+ def pyramid (n : int ) -> None :
33+ """Prints a pyramid pattern of height n."""
34+ for i in range (n ):
35+ print (" " * (n - i - 1 ) + "*" * (2 * i + 1 ))
36+
37+
38+ def inverted_pyramid (n : int ) -> None :
39+ """Prints an inverted pyramid pattern of height n."""
40+ for i in range (n - 1 , - 1 , - 1 ):
41+ print (" " * (n - i - 1 ) + "*" * (2 * i + 1 ))
42+
43+
44+ def diamond (n : int ) -> None :
45+ """Prints a diamond pattern of height n."""
46+ pyramid (n )
47+ for i in range (n - 2 , - 1 , - 1 ):
48+ print (" " * (n - i - 1 ) + "*" * (2 * i + 1 ))
49+
50+
51+ if __name__ == "__main__" :
52+ print ("Pyramid:" )
53+ pyramid (5 )
54+ print ("\n Inverted Pyramid:" )
55+ inverted_pyramid (5 )
56+ print ("\n Diamond:" )
57+ diamond (5 )
You can’t perform that action at this time.
0 commit comments