Creating a V-shaped pattern with Python
I want to make a template by algorithm.
The result is a pattern like
..........
..........
..........
..........
..........
..........
.########.
.########.
##########
##########
But my ideal pattern
##......##
##......##
.##....##.
.##....##.
..##..##..
..##..##..
...####...
...####...
....##....
....##....
So, I don't understand why I can't do this in my code. Comparing the current template and the ideal one, I think that the data cannot be used correctly. But I don't know how to fix it. What should I do to do this?
+3
source to share
5 answers
Another 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))
This will print:
##......##
##......##
.##....##.
.##....##.
..##..##..
..##..##..
...####...
...####...
....##....
....##....
It can be made even cleaner, but you can see all the different steps here, so I hope this helps
+1
source to share
# 10x10
# first # start position second # start position from last
# LN 0 1: 0 -1
# LN 2 3: 1 -2
# LN 2k 2k+1: k -k-1
# LN 8 9: 4 -5
# dw: dot '.' width
# sw: sharp '#' width
# if k == 0 part is ugly, for list slicing
def draw(dw, sw):
for k in range(dw//2):
row = [ '.' for _ in range(dw) ]
row[k:k+sw] = '#' * sw
if k == 0:
row[-sw:] = '#' * sw
else:
row[-k-sw:-k] = '#' * sw
print("".join(row))
print("".join(row))
draw(10, 2)
since draw(20, 2)
received
##................##
##................##
.##..............##.
.##..............##.
..##............##..
..##............##..
...##..........##...
...##..........##...
....##........##....
....##........##....
.....##......##.....
.....##......##.....
......##....##......
......##....##......
.......##..##.......
.......##..##.......
........####........
........####........
.........##.........
.........##.........
0
source to share
I would use the itertools product and filter out the indices that don't matter
def v(row):
return set([int(row/4), row-int(row/4), int(row/4)+1, row-int(row/4)-1])
from itertools import product
indices = product(range(len(array)),range(len(array[0])))
indices = filter(lambda i: i[1] in v(i[0]), indicies)
for r,c in indices:
array[r][c] = "#"
0
source to share