Given: A positive integer n≤6.
Return: The total number of signed permutations of length n, followed by a list of all such permutations (you may list the signed permutations in any order).
Return: The total number of signed permutations of length n, followed by a list of all such permutations (you may list the signed permutations in any order).
import copy import math def permutations(my_set, perm): if len(my_set) > 0: res = [] for s in my_set: new_my_set = copy.deepcopy(my_set) new_my_set.remove(s) for j in permutations(new_my_set, perm + str(s)): res.append(j) return res else: return [perm] def lenn_sets(my_set, res, k): if k > 1: new_res = [] for r in range(len(res)): for i in my_set: new_res.append(res[r] + i) return lenn_sets(my_set, new_res, k-1) else: return res n = 6r1 = permutations(list(range(1,n+1)), '') my_set2 = ['+', '-'] r2 = lenn_sets(my_set2, copy.deepcopy(my_set2), n) f = open('sign.txt', 'w') f.write(str(int(math.factorial(n)*math.pow(2, n)))+'\n') for r_1 in r1: for r_2 in r2: l = '' for i in range(n): l += r_2[i] + r_1[i] + ' ' f.write(l.replace('+', '')+'\n') f.close()
Комментариев нет:
Отправить комментарий