题目:
You are given the length of the permutation (an integer) and the permutation index (an integer starting from 1). The task is to calculate one of the permutations of the consecutive integers (starting from 0) and return it (an iterable of integers).
For example
Input: 3, 4
All consecutive permutations are:
(0, 1, 2)
(0, 2, 1)
(1, 0, 2)
(1, 2, 0) !!!
(2, 0, 1)
(2, 1, 0)
Output: (1, 2, 0)
Example:
assert tuple(reversed_permutation_index(3, 5)) == (2, 0, 1)
assert tuple(reversed_permutation_index(6, 271)) == (2, 1, 3, 0, 4, 5)
链接:
https://py.checkio.org/en/mission/reversed-permutation-index/
代码:
from typing import Iterable
def reversed_permutation_index(length: int, index: int) -> Iterable[int]:
import numpy as np
from itertools import permutations
a = np.arange(0,length)
for i in permutations(a):
if list(permutations(a)).index(i) == index-1 :
r = i
break
return r