代码之家  ›  专栏  ›  技术社区  ›  Programer Beginner

Python矩阵,从数和对得到cordinate

  •  -2
  • Programer Beginner  · 技术社区  · 8 年前

    问题是:

    我有一个矩阵 X 列数和 Y 行数。

    瓷砖的标签来自 从左到右 , 自上而下 一个接一个按升序排列的数字。

    在一个 蟒蛇 ,马修,我怎样才能得到一个N的坐标?如何从坐标系中得到一个N数?


    例子:

    我有一个矩阵 columns(X) = 5 row(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 回复  |  直到 8 年前
        1
  •  0
  •   Reblochon Masque    8 年前

    下面的小助手将把矩阵中的每个索引转换为它的行-列坐标(基于零,如python)

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

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