如何在此代码中为数字0创建矩阵?

时间:2013-11-18 22:59:16

标签: python python-2.7

大家好,我有一个家庭作业来绘制给定高度的金字塔。以下是height = 2和height = 3的示例:

.....         .....
.....         .....
.....         ..x..
..X..         .xxx.
.XXX. n = 2   xxxxx n = 3

这是我正在使用的代码。我的问题是让0工作:

heights = [3,4,0,5,2]

def buildPyramid(height):
    base = 2 * height - 1
    grid = []

    if height == 0:
        pass

    else:
        for xnum in range(base, 0, -2):
            # Determine the number of dots
            dots = int((base - xnum)/2)

            # Create new row in the grid
            line = ['.']*dots + ['X']*xnum + ['.']*dots

            # insert new row
            grid.insert(0, line)

        #Add 'sky' rows
        for i in range(5 - height):
            grid.insert(0, ['.']*base)
    return grid

# Print pyramids.
def print_grid(grid):

    # 1. Create all pyramids and add all of them to a list
    # 2. For 5 rows
        # 2b print the row from p0, p1, p2, p3, p4

    for pyr in grid:
        for row in pyr:
            print (" ".join(row))
pyramidList = []

for h in heights:
    pyramidList.append( buildPyramid(h) )

print_grid(pyramidList)

2 个答案:

答案 0 :(得分:0)

如何建造高度为0的金字塔?好吧,你可以简单地输出一个句点网格来表明它没有被填充。要做到这一点,你需要处理height == 0条件,你只需在代码中传递它。这是它应该是什么样子:

def buildPyramid(height):
    base = 2 * height - 1
    grid = []
    if height <= 0:  # Handle a negative or zero height.
        # Use a 5x5 grid of emptiness (periods)
        return ['.'*5] * 5
    else:
        for xnum in range(base, 0, -2):
            # Determine the number of dots
            dots = int((base - xnum)/2)
            # Create new row in the grid
            line = ['.']*dots + ['X']*xnum + ['.']*dots
            # insert new row
            grid.insert(0, line)
        #Add 'sky' rows
        for i in range(5 - height):
            grid.insert(0, ['.']*base)
    return grid

这样做,您的输出现在看起来像这样:

>>> print_grid(pyramidList)
. . . . .
. . . . .
. . X . .
. X X X .
X X X X X
. . . . . . .
. . . X . . .
. . X X X . .
. X X X X X .
X X X X X X X
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . X . . . .
. . . X X X . . .
. . X X X X X . .
. X X X X X X X .
X X X X X X X X X
. . .
. . .
. . .
. X .
X X X

答案 1 :(得分:0)

我认为您需要取消添加“天空”行的代码,以便无条件地运行,而不是仅针对大于1的高度运行。然后,您只需要修复base变量,以获得零高度金字塔的有意义值。类似的东西:

def buildPyramid(height):
    grid = []

    if height ==0:
        base = 1                      # added this line, replacing "pass"

    else:
        base = 2 * height - 1         # moved this down a bit

        for xnum in range(base, 0, -2):

            # Determine the number of dots
            dots = int((base - xnum)/2)

            # Create new row in the grid
            line = ['.']*dots + ['X']*xnum + ['.']*dots

            # insert new row
            grid.insert(0, line)

    #Add 'sky' rows
    for i in range(5 - height):                           # unindented these lines
        grid.insert(0, ['.']*base)


    return grid

进一步改进(与零高度问题无关)将更改您的代码,以便您可以使用grid.append(line)而不是grid.insert(0, line)。附加到列表是(平均)一个恒定时间操作,而在开始时插入需要复制整个列表。一种可能的解决方案是颠倒构建列表(只需用insert次调用替换append次调用),然后在返回之前反转列表。另一种选择是首先添加天空行(使用append),然后在金字塔行后面添加一个向上计数而不是向下计数的循环:for xnum in range(1, base+1, 2)