# problem 24
# Lexicographic permutations
"""
A permutation is an ordered arrangement of objects. 
For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. 
If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. 
The lexicographic permutations of 0, 1 and 2 are:

012   021   102   120   201   210

What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
"""
import operator as op
import functools as fn
def factorial(x):
    return fn.reduce(op.mul, range(1,x+1), 1)

def lexicographic_permutations(number:int):
    """Finds all permutations of the given number string"""
    count = factorial(len(number))
    numbers = list(number)
    
    permutations = set([number])

lexicographic_permutations('012')
print([i for i in range(3, 3-3, -1)])
def permutations(iterable, r=None):
    # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
    # permutations(range(3)) --> 012 021 102 120 201 210
    pool = tuple(iterable)
    n = len(pool)
    r = n if r is None else r
    if r > n:
        return
    indices = list(range(n))
    cycles = list(i for i in range(n, n-r, -1))
    yield tuple(pool[i] for i in indices[:r])
    while n:
        for i in reversed(range(r)):
            cycles[i] -= 1
            if cycles[i] == 0:
                indices[i:] = indices[i+1:] + indices[i:i+1]
                cycles[i] = n - i
            else:
                j = cycles[i]
                indices[i], indices[-j] = indices[-j], indices[i]
                yield tuple(pool[i] for i in indices[:r])
                break
        else:
            return
print([i for i in permutations(range(3))])
# problem 24
# Lexicographic permutations
"""
A permutation is an ordered arrangement of objects. 
For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. 
If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. 
The lexicographic permutations of 0, 1 and 2 are:

012   021   102   120   201   210

What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
"""
import operator as op
import functools as fn
def factorial(x):
    return fn.reduce(op.mul, range(1,x+1), 1)

def lexicographic_permutations(number:int):
    """Finds all permutations of the given number string"""
    count = factorial(len(number))
    numbers = list(number)
    
    permutations = set([number])

lexicographic_permutations('012')
print([i for i in range(3, 3-3, -1)])
def permutations(iterable, r=None):
    # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
    # permutations(range(3)) --> 012 021 102 120 201 210
    pool = tuple(iterable)
    n = len(pool)
    r = n if r is None else r
    if r > n:
        return
    indices = list(range(n))
    cycles = list(i for i in range(n, n-r, -1))
    yield tuple(pool[i] for i in indices[:r])
    while n:
        for i in reversed(range(r)):
            cycles[i] -= 1
            if cycles[i] == 0:
                indices[i:] = indices[i+1:] + indices[i:i+1]
                cycles[i] = n - i
            else:
                j = cycles[i]
                indices[i], indices[-j] = indices[-j], indices[i]
                yield tuple(pool[i] for i in indices[:r])
                break
        else:
            return
print([i for i in permutations(range(3))])
# problem 24 # Lexicographic permutations A permutation is an ordered arrangement of objects.  For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4.  If all of the permutations are listed numerically or alphabetically, we call it lexicographic order.  The lexicographic permutations of 0, 1 and 2 are:  012   021   102   120   201   210  What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9? Finds all permutations of the given number string # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC # permutations(range(3)) --> 012 021 102 120 201 210