生成NXN螺旋

时间:2015-08-05 12:41:22

标签: python

我被赋予了在python中创建螺旋的任务,用户输入一个数字,例如3,它将输出一个3x3螺旋,如下所示:

- - \
/ \ |
\ - /

我不是在寻找完整的代码我根本不知道如何去做,显然使用if语句打印出所有可能的解决方案是不可能的或逻辑的。这里真正的问题是我应该做什么,for循环,定义我自己的功能?有没有人可以链接我的文件,这将有所帮助。完整的任务大纲如下:

  

这里的任务是编写一个程序来绘制给定大小的螺旋   在一个盒子里面。

     

您的程序应该询问用户一个表示盒子大小的正整数。然后你的程序应该打印出来   那个大小的盒子里面的螺旋形。

     

例如:

Enter size: 3
- - \
/ \ |
\ - /
     

Enter size: 4
- - - \
/ - \ |
| \ / |
\ - - /
     

Enter size: 5     
- - - - \
/ - - \ |
| / \ | |
| \ - / |
\ - - - /
     

输入大小始终大于1.

5 个答案:

答案 0 :(得分:15)

我尊重你不想要完整的代码。这故意只是部分答案。

首先制作一个二维数组。类似的东西:

grid = [[None]*n for i in range(n)]

这允许您编写像grid[i][j] = '\'这样的代码。

i,j = 0,0开始。在围绕网格的循环螺旋中。有一个变量direction可能会有所帮助,它带有值'right', 'left', 'up', 'down'以及相应的delta(0,1)(向右移动)以添加到(i,j)实施此举措。

沿某条方向行,放置' - '或'|'直到你到达角落(检查None以及整个网格的限制)。当你到达一个角落时,放置适当的角标记并改变方向。

填充网格后,使用空字符串分隔符连接每一行,并将结果与​​'\n'作为分隔符连接。

答案 1 :(得分:14)

注意事项:

  • 行/列中的字符数为 n
  • 第一行将始终包含 n - 1 -和一个\
  • 最后一行始终为 n - 2 -,以\开头,以/
  • 结尾

例如,当n为4时:

第一行:- - - \
最后一行:\ - - /

可以使用以下方式轻松实现:

def get_first_raw(n):
    return '- ' * (n - 1) + '\\'

def get_last_raw(n):
    return '\\ ' + '- ' * (n - 2) + '/'

现在关于螺旋体,请注意以下几点:

对于n = 3:

- - \
/ \ |
\ - /

对于n = 5:

enter image description here

对于n = 6:

enter image description here

请注意,其中的4螺旋包含,红色框固定。只有他们的长度根据 n 而变化。

它包含在里面。在 n = 7 之前, n = 5 包含在其中。同样适用于 n = 2k ,每个n都包含 n / 2 螺旋。

我在这里试图说明你手动绘制 n = 3 n = 2 。如果螺旋应该是偶数,你可以使用 n = 2 模式,构造第一行和最后一行,并使用循环来附加螺旋体。

n = 5 的示例:

def get_spiral(n):
    res = []
    res.append(get_first_raw(n))
    res.append('/ ' + spiral[0] + ' |')
    for line in spiral[1:]:
        res.append('| ' + line + ' |')

    res.append(get_last_raw(n))
    return res

print '\n'.join(get_spiral(5))

其中spiral是大小为3的初始螺旋:

spiral = ['- - \\', '/ \ |', '\ - /']

为了生成7螺旋,你可以:

spiral = build_spiral(5)
print '\n'.join(build_spiral(7))

你会得到:

- - - - - - \
/ - - - - \ |
| / - - \ | |
| | / \ | | |
| | \ - / | |
| \ - - - / |
\ - - - - - /

当然这可以改进,你可以让程序更有效率,我只想给你一个指导并分享我的想法..

这里有更多的乐趣:

- - - - - - - - - - \
/ - - - - - - - - \ |
| / - - - - - - \ | |
| | / - - - - \ | | |
| | | / - - \ | | | |
| | | | / \ | | | | |
| | | | \ - / | | | |
| | | \ - - - / | | |
| | \ - - - - - / | |
| \ - - - - - - - / |
\ - - - - - - - - - /
- - - - - - - - - - - - - - - - - - - - - - - - \
/ - - - - - - - - - - - - - - - - - - - - - - \ |
| / - - - - - - - - - - - - - - - - - - - - \ | |
| | / - - - - - - - - - - - - - - - - - - \ | | |
| | | / - - - - - - - - - - - - - - - - \ | | | |
| | | | / - - - - - - - - - - - - - - \ | | | | |
| | | | | / - - - - - - - - - - - - \ | | | | | |
| | | | | | / - - - - - - - - - - \ | | | | | | |
| | | | | | | / - - - - - - - - \ | | | | | | | |
| | | | | | | | / - - - - - - \ | | | | | | | | |
| | | | | | | | | / - - - - \ | | | | | | | | | |
| | | | | | | | | | / - - \ | | | | | | | | | | |
| | | | | | | | | | | / \ | | | | | | | | | | | |
| | | | | | | | | | | \ - / | | | | | | | | | | |
| | | | | | | | | | \ - - - / | | | | | | | | | |
| | | | | | | | | \ - - - - - / | | | | | | | | |
| | | | | | | | \ - - - - - - - / | | | | | | | |
| | | | | | | \ - - - - - - - - - / | | | | | | |
| | | | | | \ - - - - - - - - - - - / | | | | | |
| | | | | \ - - - - - - - - - - - - - / | | | | |
| | | | \ - - - - - - - - - - - - - - - / | | | |
| | | \ - - - - - - - - - - - - - - - - - / | | |
| | \ - - - - - - - - - - - - - - - - - - - / | |
| \ - - - - - - - - - - - - - - - - - - - - - / |
\ - - - - - - - - - - - - - - - - - - - - - - - /

您还可以免费获得金字塔的顶视图

答案 2 :(得分:1)

代码:

def rot_right(a):
    return zip(*a[::-1])


def spiral(m, n, start=1, is_rotate=False):
    if n < 1:
        return

    lst = ['|' if is_rotate else '-' for i in range(start, m + start - 1)]
    lst += ['/' if is_rotate and m > 0 else '\\']


    yield tuple(lst)
    for row in rot_right(list(spiral(n - 1, m, m + start, not is_rotate))):
        yield row

n = 7
for row in spiral(n, n):
    print(''.join('%s' % i for i in row))

结果:

------\
/----\|
|/--\||
||/\|||
||\-/||
|\---/|
\-----/

-------\
/-----\|
|/---\||
||/-\|||
|||\/|||
||\--/||
|\----/|
\------/

答案 3 :(得分:1)

由于已经有一些很好的答案如何做到这一点,我按照我喜欢的方式进行并制作了代码,这是以螺旋顺序行进并沿途设置角色。它有点长但是为了理解目的,可以缩短一半:

n = 3;

grid = [[None]*n for i in range(n)]


def move(pos, d, counter): # first paint, then move
    x = pos[0]
    y=pos[1]

    #uncomment this line to check how it moves
    #print (x,y)

    if d == "right":
        if x == n-y-1: # if we are going right and we reach the end(n-y) -1 because of indexes, change direction to down
            grid[y][x] = "\\"
            y+=1
            d = "down"
        else:
            grid[y][x] = "-"
            x+=1
    elif d == "down": # if we are going down and reach the end, which is the same as column number we are on, change direction to left
        if y == x:
            grid[y][x] = "/"
            x-=1
            d = "left"
        else:
            grid[y][x] = "|"
            y+=1
    elif d == "left": # if we are going left and reach the end, which is the same as in right, change directiont to up
        if x == n-y-1:
            grid[y][x] = "\\"
            y-=1
            d="up"
        else:
            grid[y][x] = "-"
            x-=1
    elif d == "up": # if we are going up and reach the end, which is x+1, change direction to right
        if y ==x+1:
            grid[y][x] = "/"
            x+=1
            d = "right"
        else:
            grid[y][x] = "|"
            y-=1

    counter+=1
    if counter != n*n: # if we painted n*n times, it means we finished with the spiral
        move((x,y),d,counter)


move((0,0),"right",0) # start in coords (0,0) with direction going right and counter in 0

for row in grid:
    print(''.join('%s' % i for i in row))

答案 4 :(得分:1)

我不知道python,但是当n的较大值用于创建螺旋时,很明显螺旋是由3个方程组成的系统及其不等式。有三个线方程对应于\/的用法:

\ is y1 = -x + (n - 1)
/ is y2 = x + 1
     y3 = x

 0,0
  - - - - - - - \y1
y2/ - - - - - \ |
  | / - - - \ | |
  | | / - \ | | |
  | | | \ / | | |
  | | \ - - / | |
  | \ - - - - / |
  \ - - - - - - /y3

然后,遍历网格中的所有点,计算给定x的y1,y2和y3。给定y的当前值与y1,y2和y3相比,输出正确的字符。

如果它在一条线上,那么它是\/。如果它低于y1和y2,或者高于y1和y3,那么它是-,否则它是| (C#示例)

for (int y = 0; y < n; y++)
{
    for (int x = 0; x < n; x++)
    {
        string c = "  ";
        int y1 = (n - 1) + (-1 * x);
        int y2 = x + 1;
        int y3 = x; // Redundant

        if (y == y1)
            c = "\\ ";
        else if ((y <= n / 2 && y == y2) || (y >= n / 2 && y == y3))
            c = "/ ";
        else if (y < y1 && y < y2)
            c = "- ";
        else if (y > y1 && y > y3)
            c = "- ";
        else
            c = "| ";

        Console.Write(c);
    }
    Console.Write('\n');
}