Python矩阵,从数字和相反方向获取坐标

时间:2018-08-05 11:19:33

标签: python math matrix logic coordinates

问题:

我有一个矩阵,其列数为X,行数为Y

从左到右,从上到下分别标记图块,并按从小到大的顺序编号。

以数学方式 Pythonic ,如何获取N个数字的坐标?如何从坐标中获得N号?


示例:

我有一个columns(X) = 5row(y) = 6的矩阵

是这样的:

+----+----+----+----+----+
| 1  | 2  | 3  | 4  | 5  |
+----+----+----+----+----+
|  6 |  7 |  8 |  9 | 10 |
+----+----+----+----+----+
| 11 | 12 | 13 | 14 | 15 |
+----+----+----+----+----+
| 16 | 17 | 18 | 19 | 20 |
+----+----+----+----+----+
| 21 | 22 | 23 | 24 | 25 |
+----+----+----+----+----+
| 26 | 27 | 28 | 29 | 30 |
+----+----+----+----+----+

示例代码:

COLUMN_X = 5
ROW_Y = 6

def cord_to_n(column, row):
    # do the magic
    return n

def n_to_cord(n):
    # do the magic
    return column, row

预期输出:

>>> cord_to_n(2,4) # 2nd column, 4th row
17

>>> n_to_cord(23)
(3,5) # 3rd column, 5th row

注意:

我更喜欢不使用任何库的解决方案,而是使用 python数学运算符


编辑

1)这不是我的家庭作业,不是我的自学程序,我的学校不提供计算机科学。

2)这是我的尝试:

def magic(n):
    n = int(n)
    x = n % COLUMN_X # column number
    y = n/ROW_Y + 1 # row number
    return y,x

有一阵子我以为它可以用,但是发现如果我要搜索的号码在最后一列中就不起作用了。

1 个答案:

答案 0 :(得分:0)

以下小型帮助程序会将矩阵中的每个索引转换为其行列坐标(基于python,从零开始)

[编辑] ->如果希望行和列索引从1开始,可以执行以下操作:

row, col = convert_to_base1(get_row_col(idx, rows, cols))

以下是代码和测试:

def convert_to_base1(args):
    row, col = args
    return row + 1, col + 1

def get_row_col(idx, rows, cols):
    """ translates a provided index to its row col coordinates

    idx: int, the index to translate
    rows: int, the number of rows
    cols: int, the number of columns
    """
    ndx = idx - 1
    row = ndx // cols
    col = ndx % cols
    return row, col

def test_get_row_col():
    assert get_row_col(22, 6, 5) == (4, 1)
    assert get_row_col(5, 6, 5) == (0, 4)
    assert get_row_col(6, 6, 5) == (1, 0)
    assert get_row_col(10, 6, 5) == (1, 4)
    assert get_row_col(1, 6, 5) == (0, 0)
    assert get_row_col(11, 6, 5) == (2, 0)
    assert get_row_col(13, 6, 5) == (2, 2)
    assert get_row_col(30, 6, 5) == (5, 4)
    assert get_row_col(26, 6, 5) == (5, 0)

    assert get_row_col(26, 1, 30) == (0, 25)
    assert get_row_col(26, 10, 10) == (2, 5)
    assert get_row_col(26, 7, 4) == (6, 1)
    assert get_row_col(26, 4, 7) == (3, 4)
    print('***all test_get_row_col pass***')

def test_get_row_col_convert_to_base1():
    assert convert_to_base1(get_row_col(22, 6, 5)) == (5, 2)
    assert convert_to_base1(get_row_col(5, 6, 5)) == (1, 5)
    assert convert_to_base1(get_row_col(6, 6, 5)) == (2, 1)
    assert convert_to_base1(get_row_col(10, 6, 5)) == (2, 5)
    assert convert_to_base1(get_row_col(1, 6, 5)) == (1, 1)
    assert convert_to_base1(get_row_col(11, 6, 5)) == (3, 1)
    assert convert_to_base1(get_row_col(13, 6, 5)) == (3, 3)
    assert convert_to_base1(get_row_col(30, 6, 5)) == (6, 5)
    assert convert_to_base1(get_row_col(26, 6, 5)) == (6, 1)

    assert convert_to_base1(get_row_col(26, 1, 30)) == (1, 26)
    assert convert_to_base1(get_row_col(26, 10, 10)) == (3, 6)
    assert convert_to_base1(get_row_col(26, 7, 4)) == (7, 2)
    assert convert_to_base1(get_row_col(26, 4, 7)) == (4, 5)
    print('***all test_get_row_col_convert_to_base1 pass***')


test_get_row_col()
test_get_row_col_convert_to_base1()