
I want this pattern, what’s the logic behind this one? There are lots of blogs online that show code but not teach technique. Share any blogs that you know that teach the logic behind it as well.
At i=1, j=5 should be printed as star, rest as white spaces
At i=2, j=4,6…
At i=3, j=3,5,7….
At i=4,j=2,4,6,8…
At i=5, j=1,3,5,7,9…
How do I convert this into logic?
When solving this print pattern problem, try to think of it as rows and columns:
So, you have 5 columns and 5 rows

In some places where columns and rows intersect, you wan to print spaces, and in other places you want to print stars. Consider the following code:
rows = 5
for row in range(rows):
for columns in range(row):
print('* ', end=' ')
print()
Output
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Now you have to visualize and plan where you want to print a star or where you want to print a space.
columns = 5
for row in range(1, columns + 1):
print(' ' * (columns - row) + '* ' * row)
Output
*
* *
* * *
* * * *
* * * * *
Explanation
For every row in range(1, 6), print the following pattern, print(’ ’ * (columns - row) + ’ * ’ * row)
So the print pattern will look like this, row by row:
print(' ' * (5 - 1) + '* ' * 1)
print(' ' * (5 - 2) + '* ' * 2)
print(' ' * (5 - 3) + '* ' * 3)
print(' ' * (5 - 4) + '* ' * 4)
print(' ' * (5 - 5) + '* ' * 5)
Almost all pattern printing can be solved by thinking of them in rows and columns in this manner. Let me know if this helps explain the logic behind pattern printing.
2 Likes
thank you. I got it. Solved.