Skip to content

Commit c0e2df5

Browse files
authored
Merge pull request larymak#272 from LpCodes/Star-Pattern
Star pattern
2 parents 61bd2cb + fc110fe commit c0e2df5

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

OTHERS/Star pattern/readme.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Patterns in Python
2+
This is a Python script that generates five different patterns using loops and print statements. The patterns are printed to the console.
3+
4+
## Pattern 1
5+
The first pattern is a triangle of asterisks, with each row having one more asterisk than the previous row:
6+
7+
```
8+
*
9+
**
10+
***
11+
****
12+
*****
13+
```
14+
The pattern is generated using a nested loop, with the outer loop iterating over the rows and the inner loop iterating over the columns in each row.
15+
16+
## Pattern 2
17+
The second pattern is an upside-down triangle of asterisks, with each row having one less asterisk than the previous row:
18+
19+
```
20+
*****
21+
****
22+
***
23+
**
24+
*
25+
```
26+
27+
The pattern is generated using a nested loop similar to the first pattern, but with the rows iterating in reverse order.
28+
29+
## Pattern 3
30+
The third pattern is a triangle of asterisks with leading spaces, with each row having one more asterisk than the previous row and one fewer space than the previous row:
31+
32+
```
33+
*
34+
**
35+
***
36+
****
37+
*****
38+
```
39+
The pattern is generated using two nested loops, with the outer loop iterating over the rows and the inner loops iterating over the columns in each row. The number of spaces and asterisks printed in each row is determined based on the current row number.
40+
41+
## Pattern 4
42+
The fourth pattern is an upside-down triangle of asterisks with leading spaces, with each row having one less asterisk than the previous row and one more space than the previous row:
43+
44+
```
45+
*****
46+
****
47+
***
48+
**
49+
*
50+
```
51+
The pattern is generated using two nested loops similar to the third pattern, but with the rows iterating in reverse order.
52+
53+
## Pattern 5
54+
The fifth pattern is a diamond made of asterisks and spaces, with the widest point of the diamond having nine asterisks and the narrowest point having one asterisk:
55+
56+
```
57+
*
58+
***
59+
*****
60+
*******
61+
*********
62+
*********
63+
*******
64+
*****
65+
***
66+
*
67+
```
68+
The pattern is generated using three nested loops, with the outer loop iterating over the rows and the inner loops iterating over the columns in each row. The number of spaces and asterisks printed in each row is determined based on the current row number.

OTHERS/Star pattern/star_pattern.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Pattern 1
2+
# *
3+
# **
4+
# ***
5+
# ****
6+
# *****
7+
8+
# Loop through the rows from 0 to 5 (6 rows in total)
9+
for i in range(6):
10+
# Loop through the columns from 0 to i (inclusive) for each row
11+
for k in range(i+1):
12+
# Print a star with no newline character
13+
print("*", end="")
14+
# Move to the next line
15+
print()
16+
17+
# Pattern 2
18+
# *****
19+
# ****
20+
# ***
21+
# **
22+
# *
23+
24+
# Loop through the rows from 5 to 1 (inclusive)
25+
for i in range(5, 0, -1):
26+
# Loop through the columns from 0 to i (inclusive) for each row
27+
for k in range(i):
28+
# Print a star with no newline character
29+
print("*", end="")
30+
# Move to the next line
31+
print()
32+
33+
# Pattern 3
34+
# *
35+
# **
36+
# ***
37+
# ****
38+
# *****
39+
40+
# Loop through the rows from 0 to 4 (5 rows in total)
41+
for i in range(5):
42+
# Loop through the columns from 1 to 5 for each row
43+
for j in range(1, 6):
44+
# Print a space with no newline character if j is less than or equal to 5-i
45+
if j <= 5-i:
46+
print(" ", end="")
47+
else:
48+
# Otherwise, print a star with no newline character
49+
print("*", end="")
50+
# Move to the next line
51+
print()
52+
53+
# Pattern 4
54+
# *****
55+
# ****
56+
# ***
57+
# **
58+
# *
59+
60+
# Loop through the rows from 5 to 1 (inclusive)
61+
for i in range(5, 0, -1):
62+
# Loop through the columns from 1 to 5 for each row
63+
for j in range(1, 6):
64+
# Print a space with no newline character if j is less than or equal to 5-i
65+
if j <= 5-i:
66+
print(" ", end="")
67+
else:
68+
# Otherwise, print a star with no newline character
69+
print("*", end="")
70+
# Move to the next line
71+
print()
72+
73+
# Pattern 5
74+
# *
75+
# ***
76+
# *****
77+
# *******
78+
# *********
79+
# *********
80+
# *******
81+
# *****
82+
# ***
83+
# *
84+
85+
# Loop through the rows from 0 to 8 (9 rows in total)
86+
for i in range(9):
87+
# Determine the number of spaces and stars to print for this row
88+
if i <= 4:
89+
spaces = 4 - i
90+
stars = 2*i + 1
91+
else:
92+
spaces = i - 4
93+
stars = 17 - 2*i
94+
# Loop through the columns for each row
95+
for j in range(1, 11):
96+
# Print spaces or stars as appropriate for the current column
97+
if j <= spaces:
98+
print(" ", end="")
99+
elif j <= spaces + stars:
100+
print("*", end="")
101+
# Move to the next line
102+
print()

0 commit comments

Comments
 (0)