Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a V-shaped pattern with Python

Tags:

python

I wanna make pattern by algorithm.

Result is the pattern like

..........
..........
..........
..........
..........
..........
.########.
.########.
##########
##########

But my ideal pattern is

##......##
##......##
.##....##.
.##....##.
..##..##..
..##..##..
...####...
...####...
....##....
....##....

So,I cannot understand why I cannot do it in my code. By comparing current pattern & ideal one,I think data cannot use correctly. But I cannot know how to fix this. What should I do to do it?

like image 363
mikimiki Avatar asked May 17 '26 21:05

mikimiki


1 Answers

A different approach:

final = []
for i in range(10):
    temp = ["." for j in range(10)]
    temp[int(i / 2)] = "#"
    temp[int(i / 2) + 1] = "#"
    temp[-int(i / 2) - 1] = "#"
    temp[-int(i / 2) -2] = "#"
    final.append(temp)

for a in final:
    print("".join(a))

Will print:

##......##
##......##
.##....##.
.##....##.
..##..##..
..##..##..
...####...
...####...
....##....
....##....

This can be made even cleaner, but here you can see all the different steps, so I hope it helps

like image 155
Leon Z. Avatar answered May 20 '26 11:05

Leon Z.