Apr-09-2021, 06:48 AM
(This post was last modified: Apr-09-2021, 06:49 AM by SantiagoPB.)
(Apr-08-2021, 09:47 PM)GOTO10 Wrote:(Apr-08-2021, 09:17 PM)SantiagoPB Wrote: I am trying to give the length of the list using n variable
I guess what I mean is, "Why did you think this would work?" Writing the recursive function requires some thought, and you accomplished that, so I'm trying to understand what the logic was behind your approach. What if you start by just assuming that the integer input will be 1 and the string will be 3 characters long, and figure out how you'd code that solution?
(Disclaimer: I haven't tried to code a solution and don't have a specific approach in mind.)
I found the solution!!! Anyway thanks for your messages. It works like I did it, was just a problem in the print( ) function.
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