mikimiki
mikimiki

Reputation: 187

Making a V-shaped pattern with 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?

Upvotes: 3

Views: 1197

Answers (5)

Uri Goren
Uri Goren

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

Martin Evans
Martin Evans

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

Headcrab
Headcrab

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

delta
delta

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

Leon Z.
Leon Z.

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

Related Questions