Given a string and an array of index numbers, return the characters of the string rearranged to be in the order specified by the accompanying array.

Ex:

scramble('abcd', [0,3,1,2]) -> 'acdb' 

The string that you will be returning back will have: 'a' at index 0, 'b' at index 3, 'c' at index 1, 'd' at index 2, because the order of those characters maps to their corisponding numbers in the index array. 


In other words, put the first character in the string at the index described by the first element of the array



You can assume that you will be given a string and array of equal length and both containing valid characters (A-Z, a-z, or 0-9).

Source: codewars | Difficulty: EASY
Tags: String algorithms

Fundamentals

Data structures

Starter code: def scramble(string, array):
	

Solutions:
def scramble(string, array):
	return ''.join((v for (_, v) in sorted(zip(array, string))))


def scramble(string, array):
	res = array[:]
	for (i, a) in enumerate(array):
		res[a] = string[i]
	return ''.join(res)


def scramble(string, lst):
	result = [None] * len(string)
	for (char, index) in zip(string, lst):
		result[index] = char
	return ''.join(result)


def scramble(string, array):
	d = dict(list(zip(array, string)))
	return ''.join(map(d.get, list(range(len(array)))))


def scramble(stg, lst):
	return ''.join((t[1] for t in sorted(enumerate(stg), key=lambda t: lst[t[0]])))


def scramble(string, array):
	return ''.join([string[array.index(i)] for i in range(len(array))])


def scramble(string, array):
	return ''.join((c for (_, c) in sorted(zip(array, string))))


def scramble(string, array):
	return ''.join((v for (i, v) in sorted(zip(array, string))))


def scramble(string, array):
	return ''.join((string[array.index(x)] for x in range(len(string))))


def scramble(a, b):
	if not a or not b:
		return ''
	if len(a) != len(b):
		return ''
	s = sorted(zip(b, a))
	(a, b) = map(list, zip(*s))
	return ''.join(b)