A function receives a certain numbers of integers ```n1, n2, n3 ..., np```(all positive and different from 0) and a factor ```k, k > 0```

The function rearranges the numbers ```n1, n2, ..., np``` in such order that generates the minimum number concatenating the digits and this number should be divisible by ```k```.

The order that the function receives their arguments is:
```python
rearranger(k, n1, n2, n3,....,np)
```

## Examples

```python
rearranger(4, 32, 3, 34, 7, 12)  == "Rearrangement: 12, 3, 34, 7, 32 generates: 12334732 divisible by 4"

rearranger(10, 32, 3, 34, 7, 12) == "There is no possible rearrangement"
```
If there are more than one possible arrengement for the same minimum number, your code should be able to handle those cases:
```python
rearranger(6, 19, 32, 2, 124, 20, 22) == "Rearrangements: 124, 19, 20, 2, 22, 32 and 124, 19, 20, 22, 2, 32 generates: 124192022232 divisible by 6"
```

The arrangements should be in sorted order, as you see: `124, 19, 20, 2, 22, 32` comes first than `124, 19, 20, 22, 2, 32`.

Have an enjoyable time!

(Thanks to `ChristianE.Cooper` for his contribution to this kata)

Difficulty: EASY
Tags: String algorithms

Fundamentals

Sorting

Mathematics
Skill types: Sorting

Starter code:
def rearranger(k, *args):
	

Solutions:
from itertools import permutations

def rearranger(k, *args):
	perms = permutations(map(str, args), len(args))
	divisible_by_k = filter(lambda x: int(''.join(x)) % k == 0, perms)
	try:
		rearranged = min(divisible_by_k, key=lambda x: int(''.join(x)))
		return 'Rearrangement: {} generates: {} divisible by {}'.format(', '.join(rearranged), ''.join(rearranged), k)
	except ValueError:
		return 'There is no possible rearrangement'


import itertools

def rearranger(k, *args):
	(arrengment, prep) = (list(set(itertools.permutations([x for x in args]))), [])
	for (key, char) in enumerate(arrengment):
		seriesOfNumbers = [str(x) for x in arrengment[key]]
		number = int(''.join([x for x in ''.join(str(arrengment[key])) if x.isdigit()]))
		if number % k == 0:
			prep.append([seriesOfNumbers, number])
	try:
		minimum = min([x[1] for x in prep])
		answer = [x[0] for x in prep if x[1] == minimum] + [x[1] for x in prep if x[1] == minimum]
		if len(answer) == 0:
			return 'There is no possible rearrangement'
		elif len(answer) == 2:
			return 'Rearrangement: %s generates: %d divisible by %d' % (', '.join([x for x in answer[0]]), [y for (x, y) in enumerate(answer) if x % 2 != 0][0], k)
		elif len(answer) > 2:
			return 'Rearrangements: %s and %s generates: %d divisible by %d' % (', '.join([x for x in answer[0]]), ', '.join([x for x in answer[1]]), [y for (x, y) in enumerate(answer)][2], k)
	except ValueError:
		return 'There is no possible rearrangement'


from itertools import permutations

def rearranger(k, *args):
	arguments = []
	numbers = []
	min_num = 10 ** 50
	for arg in [*args]:
		arguments.append(str(arg))
	for perm in permutations(arguments):
		num = int(''.join(perm))
		if num % k == 0:
			if num == min_num and (not perm in numbers):
				numbers.append(perm)
			if num < min_num:
				min_num = num
				numbers = [perm]
	if len(numbers) == 0:
		answer = 'There is no possible rearrangement'
	elif len(numbers) == 1:
		nums = ', '.join(numbers[0])
		answer = f'Rearrangement: {nums} generates: {min_num} divisible by {k}'
	elif len(numbers) == 2:
		nums1 = ', '.join(numbers[0])
		nums2 = ', '.join(numbers[1])
		answer = f'Rearrangements: {nums1} and {nums2} generates: {min_num} divisible by {k}'
	return answer


from itertools import permutations as p

def rearranger(k, *args):
	(res, u, t) = (1e+200, len(args), (0, 0))
	for i in p(args, u):
		a = int(''.join(map(str, i)))
		if a % k == 0:
			if a < res:
				res = a
				t = i
			if a == res:
				if i < t:
					t = i
	if t == (0, 0):
		return 'There is no possible rearrangement'
	else:
		return 'Rearrangement: ' + str(t)[1:-1] + ' generates: ' + str(res) + ' divisible by ' + str(k)


from itertools import permutations

def rearranger(k, *args):
	options = sorted(permutations(args, len(args)), key=lambda option: (''.join((str(x) for x in option)), option))
	res = 'There is no possible rearrangement'
	for option in options:
		aux = int(''.join((str(x) for x in option)))
		if aux % k == 0:
			res = 'Rearrangement: {} generates: {} divisible by {}'.format(', '.join((str(x) for x in option)), aux, k)
			break
	return res


from itertools import permutations

def rearranger(k, *args):
	min_d = [1e+51, None]
	for p in permutations(args):
		n = int(''.join((str(x) for x in p)))
		if n % k == 0:
			if n < min_d[0]:
				min_d = [n, set({', '.join(map(str, p))})]
			elif n == min_d[0]:
				min_d[1].add(', '.join(map(str, p)))
	if min_d[1] is None:
		return 'There is no possible rearrangement'
	return 'Rearrangement: {0} generates: {1} divisible by {2}'.format(' and '.join(min_d[1]), min_d[0], k)


from itertools import permutations
from collections import defaultdict

def rearranger(k, *args):
	(vals, D) = (map(str, args), defaultdict(set))
	for p in permutations(vals):
		x = int(''.join(p))
		if not x % k:
			D[x].add(p)
	try:
		mini = min(D)
		res = D[mini]
		return f"Rearrangement{('s' if len(res) > 1 else '')}: {' and '.join(map(', '.join, res))} generates: {mini} divisible by {k}"
	except:
		return 'There is no possible rearrangement'


from itertools import permutations

def rearranger(k, *args):
	res = set([(int(''.join(map(str, p))), p) for p in permutations(args) if int(''.join(map(str, p))) % k == 0])
	if res:
		res = sorted(res)
		ans = []
		min_val = res[0][0]
		for (i, p) in res:
			if i != min_val:
				break
			ans.append(f"{', '.join(map(str, p))}")
		return f"Rearrangement{('s' if len(ans) > 1 else '')}: {' and '.join(ans)} {(' ' if len(ans) > 1 else '')}generates: {min_val} divisible by {k}"
	return 'There is no possible rearrangement'


import itertools

def from_list_to_string(A):
	st = ''
	for x in A:
		st += str(x)
	return st

def from_list_to_string_with_comma(A):
	st = ''
	for x in A:
		st = st + str(x) + ', '
	return st[:-2]

def rearranger(k, *args):
	min = float('inf')
	index = None
	list_of_permutations = list(itertools.permutations(args))
	for i in range(len(list_of_permutations)):
		tmp = int(from_list_to_string(list_of_permutations[i]))
		if tmp % k == 0 and tmp < min:
			min = tmp
			index = i
	if index == None:
		return 'There is no possible rearrangement'
	else:
		return 'Rearrangement: ' + str(from_list_to_string_with_comma(list_of_permutations[index])) + ' generates: ' + str(min) + ' divisible by ' + str(k)