permutation using python using recursion

def create_perm(actual_list, add_list):
    """
    Recursive function for the creation of the permutation
    """
    if len(add_list)==1:
        # If you reach the last item, print the found permutation
        # (add the 0 at the beginning)
        print([0] + actual_list + add_list)
    else:
        for i in add_list:
            # Go one step deeper by removing one item and add it to the found permutation
            new_add_list = add_list.copy()
            new_add_list.remove(i)
            # Make the recursion
            create_perm(actual_list + [i], new_add_list)
    
li = [1, 2, 3, 4]
create_perm([], li)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top