-
Notifications
You must be signed in to change notification settings - Fork 0
/
perm.py
55 lines (41 loc) · 1.39 KB
/
perm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import sys
def swap(index_a, index_b, sequence):
sequence[index_a], sequence[index_b] = sequence[index_b], sequence[index_a]
def factorial(number):
if number == 1:
return 1
else:
return number * factorial(number - 1)
def permutate(sequence, pivot):
if pivot == len(sequence) - 1:
return [sequence]
results = []
for i in range(pivot, len(sequence)):
copy = sequence.copy()
swap(pivot, i, copy)
results += permutate(copy, pivot + 1)
return results
def permutations(sequence):
permutated_sequences = permutate(sequence, 0)
return permutated_sequences
def possible_permutations(number):
amount = factorial(number)
numbers = [i + 1 for i in range(number)]
return amount, permutations(numbers)
def display_result(number, permutations):
print(amount)
for permutation in permutations:
for number in permutation:
print(number, end=' ')
print()
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Invoke the program passing a path to a file as an argument")
else:
try:
with open(sys.argv[1], 'r') as file:
number = int(file.read())
amount, permutations = possible_permutations(number)
display_result(amount, permutations)
except IOError:
print("Error while trying to open the file")