index_permute 全排列

# 这里假设全排列中的每个组合对应一个序号, 依次从0~n!
# 根据序号返回对应的组合
def index_permute(seq, index):
    copy = seq[:]
    perm = [copy.pop()]
    divider = 2
    while copy:
        index, new_index = divmod(index, divider)
        perm.insert(new_index, copy.pop())
        divider += 1
    return ''.join(perm)
numbers = list('123')
for index in range(0, 6):
    print index_permute(numbers, index)