使用Python制作V形图案

时间:2017-07-19 07:31:47

标签: python

我想通过算法制作模式。

结果是类似

的模式
..........
..........
..........
..........
..........
..........
.########.
.########.
##########
##########

但我的理想模式是

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

所以,我无法理解为什么我不能在我的代码中执行此操作。 通过比较当前的模式和理想之一,我认为数据无法正确使用。但我不知道如何解决这个问题。我该怎么办呢?

5 个答案:

答案 0 :(得分:1)

另一种方法:

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))

将打印:

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

这可以变得更干净,但在这里你可以看到所有不同的步骤,所以我希望它有所帮助

答案 1 :(得分:0)

# 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)

draw(20, 2)

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

答案 2 :(得分:0)

我会使用itertools的产品,并过滤不相关的索引

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] = "#"

答案 3 :(得分:0)

不知道你在这里想要完成什么,但我也能做到:

a = "##..."
for i in range(10):
    b = ("."*int(i/2) + a)[:5]
    print(b + b[::-1])

答案 4 :(得分:0)

稍微简洁一点:

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))