Hello! Your are given x and y and 2D array size tuple (width, height) and you have to:
Calculate the according index in 1D space (zero-based). 
Do reverse operation.

Implement:
to_1D(x, y, size):
  --returns index in 1D space
to_2D(n, size)
  --returns x and y in 2D space

1D array: [0, 1, 2, 3, 4, 5, 6, 7, 8]
2D array: [[0 -> (0,0), 1 -> (1,0), 2 -> (2,0)], 
           [3 -> (0,1), 4 -> (1,1), 5 -> (2,1)], 
           [6 -> (0,2), 7 -> (1,2), 8 -> (2,2)]]


to_1D(0, 0, (3,3)) returns 0
to_1D(1, 1, (3,3)) returns 4
to_1D(2, 2, (3,3)) returns 8
to_2D(5, (3,3)) returns (2,1)
to_2D(3, (3,3)) returns (0,1)

Assume all input are valid:

1 < width  < 500; 
1 < height < 500

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Data structures

Mathematics

Starter code: def to_1D(x, y, size):
	

Solutions:
def to_1D(x, y, size):
	return y * size[0] + x

def to_2D(n, size):
	return (n % size[0], n // size[0])


def to_1D(x, y, size):
	(w, h) = size
	return x + y * w

def to_2D(n, size):
	(w, h) = size
	return (n % w, n // w)


def to_1D(x, y, size):
	return size[0] * y + x

def to_2D(n, size):
	return divmod(n, size[0])[::-1]


(to_1D, to_2D) = (lambda x, y, s: x + y * s[0], lambda n, s: (n % s[0], n // s[0]))


def to_1D(x, y, size):
	return x + y * size[0]

def to_2D(n, size):
	c = 0
	for i in range(size[1]):
		for j in range(size[0]):
			if n == c:
				return (j, i)
			c += 1


def to_1D(x, y, s):
	return s[0] * y + x

def to_2D(n, s):
	return (n // s[0], n % s[0])[::-1]