Assume we take a number `x` and perform any one of the following operations:
```Pearl
a) Divide x by 3 (if it is divisible by 3), or
b) Multiply x by 2
```
After each operation, we write down the result. If we start with `9`, we can get a sequence such as:
```
[9,3,6,12,4,8] -- 9/3=3 -> 3*2=6 -> 6*2=12 -> 12/3=4 -> 4*2=8
```
You will be given a shuffled sequence of integers and your task is to reorder them so that they conform to the above sequence. There will always be an answer. 
```
For the above example:
solve([12,3,9,4,6,8]) = [9,3,6,12,4,8].
```

More examples in the test cases. Good luck!

Difficulty: EASY
Tags: Fundamentals

Starter code:
def solve(arr):
	

Solutions:
def solve(a):
	for i in a:
		li = [i]
		while 1:
			if li[-1] % 3 == 0 and li[-1] // 3 in a:
				li.append(li[-1] // 3)
			elif li[-1] * 2 in a:
				li.append(li[-1] * 2)
			else:
				break
		if len(li) == len(a):
			return li


def solve(arr):
	(ar, v) = (arr[:], lambda p: lambda n: 0 if n % p else 1 + v(p)(n // p))
	return sorted(reversed(sorted(ar, key=v(3))), key=v(2))


def decompose(n):
	res = [0, 0, n]
	for (i, p) in enumerate((3, 2)):
		while not n % p:
			res[i] += 1
			n //= p
	res[0] = -res[0]
	return res

def solve(arr):
	return [n for (_, _, n) in sorted(map(decompose, arr))]


def solve(arr):
	fn = lambda p: lambda n: 0 if n % p else 1 + fn(p)(n // p)
	return sorted(sorted(arr, key=fn(3), reverse=True), key=fn(2))


(k, solve) = (lambda n, p: n % p < 1 and -~k(n // p, p), lambda a: sorted(a, key=lambda n: (-k(n, 3), k(n, 2))))


def solve(arr):
	links = {}
	for a in arr:
		if a % 3 == 0:
			next_val = a // 3
			if next_val in arr:
				links[a] = next_val
		if a * 2 in arr:
			links[a] = a * 2
	start = (set(links.keys()) - set(links.values())).pop()
	result = [start]
	while True:
		next_val = links.get(result[-1])
		if next_val is None:
			break
		result.append(next_val)
	return result


def solve(arr):
	stack = []
	for num in arr:
		branches = arr.copy()
		branches.remove(num)
		stack = [([num], branches)]
		while len(stack) != 0:
			(current, remaining) = stack.pop()
			if len(remaining) == 0:
				return current
			x = current[len(current) - 1]
			valid_leaves = [leaf for leaf in remaining if x / 3 == leaf or x * 2 == leaf]
			for valid in valid_leaves:
				valid_current = current.copy()
				valid_current.append(valid)
				valid_remaining = remaining.copy()
				valid_remaining.remove(valid)
				stack.append((valid_current, valid_remaining))


def solve(arr):
	stack = []
	for i in arr:
		x = arr[:]
		x.remove(i)
		stack.append(([i], x))
	while True:
		(a, b) = stack.pop(0)
		v = a[-1]
		(a1, a2) = (a[:], a[:])
		(b1, b2) = (b[:], b[:])
		if v * 2 in b:
			a1.append(v * 2)
			b1.remove(v * 2)
			stack.append((a1, b1))
		if not v % 3:
			if v // 3 in b:
				a2.append(v // 3)
				b2.remove(v // 3)
				stack.append((a2, b2))
		if not b1:
			return a1
		if not b2:
			return a2


def solve(lst):
	return sorted(lst, key=factors_count)

def factors_count(n):
	result = []
	for k in (2, 3):
		while n % k == 0:
			n //= k
			result.append(k)
	return (-result.count(3), result.count(2))


def solve(lst):
	return sorted(lst, key=factors_count)

def factors_count(n):
	result = [0, 0]
	for k in (2, 3):
		while n % k == 0:
			n //= k
			result[k - 2] += (-1) ** k
	return result