简体   繁体   English

Python中的二维数组

[英]two-dimensional array in python

There is a task on CodeFights involving building a two-dimensional array: CodeFights有一项涉及构建二维数组的任务:

A spiral matrix is a square matrix of size n × n. 螺旋矩阵是大小为n×n的正方形矩阵。 It contains all the integers in range from 1 to n * n so that number 1 is written in the bottom right corner, and all other numbers are written in increasing order spirally in the counterclockwise direction. 它包含从1到n * n范围内的所有整数,因此数字1会写在右下角,而其他所有数字则会按逆时针方向螺旋递增的顺序写入。

Given the size of the matrix n, your task is to create a spiral matrix. 给定矩阵n的大小,您的任务是创建一个螺旋矩阵。

One is supposed to fill only one gap denoted by ... in the following code: 在下面的代码中,应该只填补一个由...表示的空白:

def createSpiralMatrix(n):
    dirs = [(-1, 0), (0, -1), (1, 0), (0, 1)]
    curDir = 0
    curPos = (n - 1, n - 1)
    res = ...

    for i in range(1, n * n + 1):
        res[curPos[0]][curPos[1]] = i
        nextPos = curPos[0] + dirs[curDir][0], curPos[1] + dirs[curDir][1]
        if not (0 <= nextPos[0] < n and
                0 <= nextPos[1] < n and
                res[nextPos[0]][nextPos[1]] == 0):
            curDir = (curDir + 1) % 4
            nextPos = curPos[0] + dirs[curDir][0], curPos[1] + dirs[curDir][1]
        curPos = nextPos

    return res

When I fill in the following code, all tests are passed: 当我填写以下代码时,所有测试均通过:

res = [[0 for item in range(n)] for sublist in range(n)]

However, if I slightly change it to: 但是,如果我将其稍微更改为:

res = [[None for item in range(n)] for sublist in range(n)]

I receive the following error message: 我收到以下错误消息:

Execution error on test 1: Something went wrong when executing the solution - program stopped unexpectedly with an error.
Traceback (most recent call last):
  file.py3 on line ?, in getUserOutputs
    userOutput = _runiuljw(testInputs[i])
  file.py3 on line ?, in _runiuljw
    return createSpiralMatrix(*_fArgs_jlosndfelxsr)
  file.py3 on line 8, in createSpiralMatrix
    res[curPos[0]][curPos[1]] = i
IndexError: list index out of range

Test 1 Input: n: 3 Output: Empty Expected Output: [[5,4,3], [6,9,2], [7,8,1]] Console Output: Empty 测试1输入:n:3输出:空预期输出:[[5,4,3],[6,9,2],[7,8,1]]控制台输出:空

The same result (with the error message) is with the following code: 相同的结果(带有错误消息)与以下代码:

res = [list(range(n)) for sublist in range(n)]

All three options build arrays of the same size: 所有这三个选项将构建相同大小的数组:

n = 3

res = [[0 for item in range(n)] for sublist in range(n)]
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]

res = [[None for item in range(n)] for sublist in range(n)]

[[None, None, None], [None, None, None], [None, None, None]]

res = [list(range(n)) for sublist in range(n)]

[[0, 1, 2], [0, 1, 2], [0, 1, 2]]

Am I missing something obvious? 我是否缺少明显的东西?

If you want it to work with None , then you simply have to change the third condition of your if not ... statement to agree with how you initialized res . 如果希望它与None ,则只需更改if not ...语句的第三个条件,以与如何初始化res达成一致。 Otherwise, nextPos will incorrectly become out of the range of your 2D array. 否则, nextPos将错误地超出2D数组的范围。

def createSpiralMatrix(n):
    dirs = [(-1, 0), (0, -1), (1, 0), (0, 1)]
    curDir = 0
    curPos = (n - 1, n - 1)
    res = [[None for item in range(n)] for sublist in range(n)]

    for i in range(1, n * n + 1):
        res[curPos[0]][curPos[1]] = i
        nextPos = curPos[0] + dirs[curDir][0], curPos[1] + dirs[curDir][1]
        if not (0 <= nextPos[0] < n and
                0 <= nextPos[1] < n and
                res[nextPos[0]][nextPos[1]] is None): # Changed this line
            curDir = (curDir + 1) % 4
            nextPos = curPos[0] + dirs[curDir][0], curPos[1] + dirs[curDir][1]
        curPos = nextPos

    return res

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM