Recursion and permutations: print all permutations filling a list of length N - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Recursion and permutations: print all permutations filling a list of length N (/thread-33212.html) |
Recursion and permutations: print all permutations filling a list of length N - SantiagoPB - Apr-06-2021 Hello everyone , 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 Recursion Coding Error - SantiagoPB - Apr-08-2021 Hello everyone , I have written a code using recursion, which follows the following rules: RULES 2 inputs:
With these two inputs, the program must do the following:
CODE My code is as follows and it fails to give the length of the output: 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(b, " ", permutation_list)) if __name__ == '__main__': main()EXAMPLE OF HOW SHOULD WORK: Input: 2 +x# Output: + x + # x + x # # + # x Could someone tell me why it doesn't work? Thank you very much for the help! RE: Recursion and permutations: print all permutations filling a list of length N - GOTO10 - Apr-08-2021 Your line 12 isn't accomplishing anything. What are you trying to do there? single_list = [] multiplied_list = [] * 10 print(f'Single: {single_list}') print(f'Multiplied: {multiplied_list}')
RE: Recursion and permutations: print all permutations filling a list of length N - SantiagoPB - Apr-08-2021 (Apr-08-2021, 08:28 PM)GOTO10 Wrote: Your line 12 isn't accomplishing anything. What are you trying to do there? I am trying to give the length of the list using n variable RE: Recursion and permutations: print all permutations filling a list of length N - GOTO10 - Apr-08-2021 (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.) RE: Recursion and permutations: print all permutations filling a list of length N - SantiagoPB - Apr-09-2021 (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 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 RE: Recursion and permutations: print all permutations filling a list of length N - GOTO10 - Apr-09-2021 (Apr-09-2021, 06:48 AM)SantiagoPB Wrote: I found the solution!!! 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.
|