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
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
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