Lexicographic permutations are ordered combinations of a set of items ordered in a specific way.

For instance, the first 8 permutations of the digits 0123, in lexicographic order, are:

```
1st 0123
2nd 0132
3rd 0213
4th 0231
5th 0312
6th 0321
7th 1023
8th 1032
```

Your task is to write a function ```L( n, d )``` that will return a `string` representing the `nth` permutation of the `d` digit, starting with 0 (i.e. d=10 means all digits 0123456789).

So for `d = 4`, `L(7,4)` should return `'1023'`, and `L(4,4)` should return `'0231'`
.


Some things to bear in mind:

• The function should return a `string`, otherwise permutations beginning with a 0 will have it removed. 

• Test cases will not exceed the highest possible valid values for `n`

• The function should work for any `d` between `1` and `10`.

• A value of 1 for `n` means the 1st permutation, so `n = 0` is not a valid input.

• Oh, and no itertools ;)

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Combinatorics

Mathematics

Starter code: def nth_perm(n,d):
	

Solutions:
import math

def nth_perm(n, d):
	digits = [str(i) for i in range(d)]
	out = ''
	for i in range(1, d):
		cycles = math.ceil(n / math.factorial(d - i))
		out += digits.pop(cycles % (d - i + 1) - 1)
	return out + digits.pop()


def nth_perm(n, d):
	(st, i, n, fc) = (list(map(str, range(d))), 1, n - 1, '')
	while n:
		(n, mod) = divmod(n, i)
		fc = str(mod) + fc
		i += 1
	return ''.join([st.pop(int(i)) for i in fc.zfill(d)])


def memoize(func):
	cache = {}

	def decorator(x):
		if not x in cache:
			cache[x] = func(x)
		return cache[x]
	return decorator

@memoize
def permutations(d):
	if d < 1:
		return []
	if d == 1:
		return ['0']
	return sorted((p[:i] + str(d - 1) + p[i:] for p in permutations(d - 1) for i in range(d)))

def nth_perm(n, d):
	return permutations(d)[n - 1]


def nth_perm(n, d):
	n -= 1
	(_in, out_, f) = ([], [], [])
	for x in range(d):
		f.append(not f or x * f[-1])
		_in.append(str(x))
	for x in range(d):
		(i, n) = divmod(n, f.pop())
		out_.append(_in.pop(i))
	return ''.join(out_)


from math import factorial

def nth_perm(n, d):
	n = n - 1
	lst = sorted(map(str, range(d)))
	f = factorial(d)
	ans = []
	while lst:
		f = f // len(lst)
		(c, n) = (n // f, n % f)
		ans += [lst.pop(c)]
	return ''.join(map(str, ans))


import sys
p = sys.modules['itertools'].permutations

def nth_perm(n, d):
	return ''.join([*p('0123456789'[:d])][~-n])


from math import factorial

def nth_perm(n, d):
	(n, ds, res) = (n - 1, list(range(d)), [])
	for i in range(d - 1, -1, -1):
		(p, n) = divmod(n, factorial(i))
		res.append(ds.pop(p))
	return ''.join(map(str, res))


def sil(a):
	b = 1
	for i in range(1, a + 1):
		b = b * i
	return b

def nth_perm(n, d):
	lit = list('0123456789'[:d])
	out = []
	for i in range(d):
		el = int((n - 1) / sil(d - 1))
		out.append(lit.pop(el))
		n = n - sil(d - 1) * el
		d -= 1
	return ''.join(out)


from math import factorial

def nth_perm(n, d):
	a = list(range(d))
	i = d - 1
	r = ''
	while i >= 0:
		b = (n - 1) // factorial(i)
		r += str(a[b])
		a.remove(a[b])
		n -= b * factorial(i)
		i -= 1
	return r