Python Forum
Recursion and permutations: print all permutations filling a list of length N
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Recursion and permutations: print all permutations filling a list of length N
#1
Question 
Hello everyone Big Grin ,

I have written a code and I need to add a condition.

The condition is to give the length of the list, for example, if the output list must be composed of N elements.
In other words, the input must be the length of the final list and the chain of elements that must be permuted in the final result. I leave the statement of the homework and the code that I have to which the length must be implemented.

CODE
# swap ith and jth character of string
def swap(s, i, j):
    q = list(s)
    q[i], q[j] = q[j], q[i]
    return ''.join(q)


# recursive function
def _permute(p, s, permutes):
    if p >= len(s) - 1:
        permutes.append(s)
        return

    for i in range(p, len(s)):
        _permute(p + 1, swap(s, p, i), permutes)


# helper function
def permute(s):
    permutes = []
    _permute(0, s, permutes)
    return permutes


# TEST IT
s = str(input("Write any string to get all the permutations without repetitions: "))
all_permute = permute(s)
print(all_permute)
ASSIGNMENT
Write a program, which reads an integer N and a sequence of distinct symbols (as a string). The
program then prints all ways how to fill a list of the length N by the symbols. The number of symbols
≥ N. Each symbol can occur at most once in a filled list.


Examples:
Input: 2
+x#
Output:
+ x
+ #
x +
x #
# +
# x

Input: 1
^+#/x
Output:
^
+
#
/
x
Reply


Messages In This Thread
Recursion and permutations: print all permutations filling a list of length N - by SantiagoPB - Apr-06-2021, 08:01 PM
Recursion Coding Error - by SantiagoPB - Apr-08-2021, 12:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Removing all strings in a list that are of x length Bruizeh 5 3,193 Aug-27-2021, 03:11 AM
Last Post: naughtyCat
  sorting a list using unicodes acending order, no loops, no sort(), using recursion lrn2codee 14 6,438 Jun-23-2021, 07:33 PM
Last Post: deanhystad
  How to print all iterations of min and max for a 2D list alaina 4 2,916 Nov-11-2020, 05:53 AM
Last Post: alaina
  Why does this function print empty list? hhydration 1 1,540 Oct-28-2020, 02:03 AM
Last Post: deanhystad
  GCF function w recursion and helper function(how do i fix this Recursion Error) hhydration 3 2,539 Oct-05-2020, 07:47 PM
Last Post: deanhystad
  List of Objects print <__main. Problem Kol789 10 3,540 Jul-21-2020, 09:37 AM
Last Post: DeaD_EyE
  How do you find the length of an element of a list? pav1983 13 4,911 Jun-13-2020, 12:06 AM
Last Post: pav1983
  How can I print the number of unique elements in a list? AnOddGirl 5 3,302 Mar-24-2020, 05:47 AM
Last Post: AnOddGirl
  Print name N times using recursion ift38375 7 7,970 Oct-23-2019, 05:33 PM
Last Post: ichabod801
  why is this not filling my empty list? Siylo 4 3,157 Jan-21-2019, 05:27 PM
Last Post: Siylo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020