Reputation: 187
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?
Upvotes: 3
Views: 1197
Reputation: 13690
I would use itertools' product, and filter the indices that aren't relevant
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] = "#"
Upvotes: 0
Reputation: 46759
Slightly more concise:
width = 10
for i in range(width):
line = ['.'] * width
m = i//2
line[m] = line[m+1] = line[-m-1] = line[-m-2] = "#"
print(''.join(line))
Upvotes: 0
Reputation: 7103
Don't know what you're trying to accomplish here, but I can do it, too:
a = "##..."
for i in range(10):
b = ("."*int(i/2) + a)[:5]
print(b + b[::-1])
Upvotes: 0
Reputation: 3818
# 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)
with draw(20, 2)
got
##................##
##................##
.##..............##.
.##..............##.
..##............##..
..##............##..
...##..........##...
...##..........##...
....##........##....
....##........##....
.....##......##.....
.....##......##.....
......##....##......
......##....##......
.......##..##.......
.......##..##.......
........####........
........####........
.........##.........
.........##.........
Upvotes: 0
Reputation: 572
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
Upvotes: 1