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
#7
(Apr-09-2021, 06:48 AM)SantiagoPB Wrote: I found the solution!!!
CODE WORKING
def stringPermutations(string, prefix, permutation_list):
    if len(string) == 0:
        permutation_list.append(prefix)
    else:
        for i in range(len(string)):
            rem = string[0:i] + string[i + 1:]
            stringPermutations(rem, prefix + string[i], permutation_list)
    return sorted(list(dict.fromkeys(permutation_list)))
def main():
    n = int(input("write size: "))
    b = str(input("Write String: "))
    permutation_list = [] * n
    print(stringPermutations(n, b, " ", permutation_list))
 
if __name__ == '__main__':
    main()
As you can see I just put the variable n inside of the print

Interesting, but your code doesn't run for me. stringPermutations expects 3 arguments but is receiving 4 in line 13, so on my system that is resulting in a TypeError.
Reply


Messages In This Thread
RE: Recursion and permutations: print all permutations filling a list of length N - by GOTO10 - Apr-09-2021, 12:36 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,086 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,245 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,831 Nov-11-2020, 05:53 AM
Last Post: alaina
  Why does this function print empty list? hhydration 1 1,501 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,485 Oct-05-2020, 07:47 PM
Last Post: deanhystad
  List of Objects print <__main. Problem Kol789 10 3,440 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,787 Jun-13-2020, 12:06 AM
Last Post: pav1983
  How can I print the number of unique elements in a list? AnOddGirl 5 3,194 Mar-24-2020, 05:47 AM
Last Post: AnOddGirl
  Print name N times using recursion ift38375 7 7,773 Oct-23-2019, 05:33 PM
Last Post: ichabod801
  why is this not filling my empty list? Siylo 4 3,095 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