The Problem:
I have a matrix that has X
number of columns and Y
number of rows.
The tiles are labeled from left to right, top to bottom with numbers in ascending order one by one.
In a Pythonic, mathy way, how can I get the coordinates of an N number? How can I get an N number from the coordinates?
Example:
I have a matrix of columns(X) = 5
and row(y) = 6
So something like this:
+----+----+----+----+----+
| 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 |
+----+----+----+----+----+
Example code:
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
The expected output:
>>> cord_to_n(2,4) # 2nd column, 4th row
17
>>> n_to_cord(23)
(3,5) # 3rd column, 5th row
Note:
I prefer a solution without using any library, but instead using python math operators.
EDIT
1) This is not my homework or anything, I am self-studying programming and my school does not offer computer science.
2) This was my attempt:
def magic(n):
n = int(n)
x = n % COLUMN_X # column number
y = n/ROW_Y + 1 # row number
return y,x
For a while I thought it worked but found out that if the number I am searching for is in the last column it does not work.