In this kata you are expected to recover a scattered password in a (m x n) grid (you'll be given directions of all password pieces in the array)

The array will contain pieces of the password to be recovered, you'll get directions on how to get all the the pieces, your initial position in the array will be the character "x".

Heres what the array looks like

The given directions would consist of `[left, right, up, down]` and `[leftT, rightT, upT, downT]`, the former would be used to move around the grid while the latter would be used when you have a password to that direction of you.(
**E.g** if you are in a position and the move to make is `leftT` it means theres a password to the left of you, so take the value and move there)

So in the 2d array example above, you will be given the directions `["lefT", "downT", "rightT", "rightT"]`, making for the word `"pa$$"`.

Remember you initial position is the character "x".

So you write the function `getPassword(grid, directions)` that uses the directions to get a password in the grid.

Another example.

Once again, Your initial position is the character "x", so from the position of "x" you follow the directions given and get all pieces in the grid.

Source: codewars | Difficulty: EASY
Tags: Data structures

Starter code: def get_password(grid,directions):
	

Solutions:
MOVES = {'right': (0, 1), 'down': (1, 0), 'left': (0, -1), 'up': (-1, 0)}

def get_password(grid, dirs):
	(x, y) = next(((x, y) for (x, r) in enumerate(grid) for (y, c) in enumerate(r) if c == 'x'))
	pwd = []
	for d in dirs:
		(dx, dy) = MOVES[d.strip('T')]
		(x, y) = (x + dx, y + dy)
		if d.endswith('T'):
			pwd.append(grid[x][y])
	return ''.join(pwd)


def get_password(grid, directions):
	(r, c) = [(r, c) for r in range(len(grid)) for c in range(len(grid[r])) if grid[r][c] == 'x'].pop()
	p = ''
	for d in directions:
		(r, c) = (r + {'d': 1, 'u': -1}.get(d[0], 0), c + {'r': 1, 'l': -1}.get(d[0], 0))
		if d[-1] == 'T':
			p += grid[r][c]
	return p


DIRS = {'left': (0, -1), 'right': (0, 1), 'up': (-1, 0), 'down': (1, 0)}

def get_password(grid, directions):
	(i, j) = next(((i, j) for (i, row) in enumerate(grid) for (j, x) in enumerate(row) if x == 'x'))
	pw = ''
	for d in directions:
		take = d.endswith('T')
		(i_delta, j_delta) = DIRS[d.rstrip('T')]
		i += i_delta
		j += j_delta
		if take:
			pw += grid[i][j]
	return pw


def get_password(grid, directions):
	res = ''
	for i in range(len(grid)):
		if 'x' in grid[i]:
			pos = [i, grid[i].index('x')]
	for i in directions:
		if i[0] == 'l':
			pos[1] -= 1
		elif i[0] == 'd':
			pos[0] += 1
		elif i[0] == 'r':
			pos[1] += 1
		else:
			pos[0] -= 1
		if i[-1] == 'T':
			res += grid[pos[0]][pos[1]]
	return res


(X, Y) = ({'l': -1, 'r': 1}, {'u': -1, 'd': 1})

def get_password(grid, directions):
	(result, (i, j)) = ([], next(((i, j) for (i, row) in enumerate(grid) for (j, c) in enumerate(row) if c == 'x')))
	for d in directions:
		(i, j) = (i + Y.get(d[0], 0), j + X.get(d[0], 0))
		if d[-1] == 'T':
			result.append(grid[i][j])
	return ''.join(result)


def get_password(grid, directions):
	m_go = {'left': (0, -1), 'right': (0, 1), 'up': (-1, 0), 'down': (1, 0)}
	(x, y) = next(((i, j) for (i, e) in enumerate(grid) for (j, l) in enumerate(e) if l == 'x'))
	password = ''
	for e in directions:
		((x_, y_), end) = (m_go.get(e.strip('T')), e[-1])
		(x, y) = (x + x_, y + y_)
		if end is 'T':
			password += grid[x][y]
	return password


moves = {'l': (-1, 0), 'u': (0, -1), 'd': (0, 1), 'r': (1, 0)}

def get_password(grid, directions):
	(pwd, (x, y)) = ('', next(((row.index('x'), j) for (j, row) in enumerate(grid) if 'x' in row)))
	for d in directions:
		(x, y) = (x + moves[d[0]][0], y + moves[d[0]][1])
		if d[-1] == 'T':
			pwd = f'{pwd}{grid[y][x]}'
	return pwd


def get_password(grid, directions):
	axis = {'left': -1, 'leftT': -1, 'right': +1, 'rightT': +1}
	ordinate = {'up': -1, 'upT': -1, 'down': +1, 'downT': +1}
	pwd = ''
	for ele in grid:
		if 'x' in ele:
			(row_index, column_index) = (grid.index(ele), ele.index('x'))
			break
	for direction in directions:
		if 't' in direction:
			column_index += axis[direction]
			if 'T' in direction:
				pwd += grid[row_index][column_index]
		else:
			row_index += ordinate[direction]
			if 'T' in direction:
				pwd += grid[row_index][column_index]
	return pwd


def get_password(grid, directions):
	start = [[k, x] for (k, v) in enumerate(grid) for (x, y) in enumerate(v) if y == 'x'][0]
	curr = start
	a = ''
	for i in directions:
		if i[-1] == 'T':
			if i == 'leftT':
				curr[1] -= 1
				a += grid[curr[0]][curr[1]]
			if i == 'rightT':
				curr[1] += 1
				a += grid[curr[0]][curr[1]]
			if i == 'upT':
				curr[0] -= 1
				a += grid[curr[0]][curr[1]]
			if i == 'downT':
				curr[0] += 1
				a += grid[curr[0]][curr[1]]
		if i[-1] != 'T':
			if i == 'left':
				curr[1] -= 1
			if i == 'right':
				curr[1] += 1
			if i == 'up':
				curr[0] -= 1
			if i == 'down':
				curr[0] += 1
	return a


def get_password(grid, directions):
	for each in grid:
		if 'x' in each:
			x = int([grid.index(each)][0])
			y = int([each.index('x')][0])
			break
	res = ''
	for each in directions:
		if each[0:2] == 'ri':
			y += 1
			if each[-1:] == 'T':
				res += grid[x][y]
		elif each[0:2] == 'le':
			y -= 1
			if each[-1:] == 'T':
				res += grid[x][y]
		elif each[0:2] == 'up':
			x -= 1
			if each[-1:] == 'T':
				res += grid[x][y]
		elif each[0:2] == 'do':
			x += 1
			if each[-1:] == 'T':
				res += grid[x][y]
	return res