I watched this video from The Unqualified Engineer on YouTube and tried out the challenge. The challenge (or interview question) is to build a generator that returns all the permutations of the positive integers up to \$n\$ (that is, \$1,2,3,\ldots,n\$).I know about itertools.permutations but want to implement it myself.def gen_perm(n):    l=tuple(range(1,n+1))    yield l    while True:        l=increment_lex(l)        if(not l):            raise StopIteration            break        yield ldef increment_lex(l):    for i in range(len(l)-1):        x=len(l)-(i+1)        y=len(l)-(i+2)        if l[x] > l[y]:            m=min([i for i in l[x:] if i > l[y]])            cl=list(l[x:])            cl[cl.index(m)] = l[y]            il=tuple(l[:y] + (m,) + tuple(cl[::-1]))            return il    returnfor i in gen_perm(3):    print(i)This outputs:(1, 2, 3)(1, 3, 2)(2, 1, 3)(2, 3, 1)(3, 1, 2)(3, 2, 1)In order to extend this to the other direction as well, however I couldn't think of a way to do it without adding two additional functions:def gen_perm_reverse(n):    l=tuple(range(n,0,-1))    yield l    while True:        l=decrement_lex(l)        if not l:            raise StopIteration            break        yield ldef decrement_lex(l):    for i in range(len(l)-1):        x=len(l)-(i+1)        y=len(l)-(i+2)        if l[x] < l[y]:            m=max([i for i in l[x:] if i < l[y]])            cl=list(l[x:])            cl[cl.index(m)] = l[y]            il=tuple(l[:y] + (m,) + tuple(cl[::-1]))            return il    returnApologies for the use of l as an identifier. It works nicely on my editor font but not here.
Create a generator that returns every permutation (unique arrangement) of positive integers up to n
python;python 3.x;functional programming;interview questions;reinventing the wheel