-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPERM.py
39 lines (32 loc) · 799 Bytes
/
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
#!/usr/bin/env python
from __future__ import division
import sys
import math
import itertools
def get_permutations(input):
n = int(input.strip())
number_of_permutations = str(math.factorial(n))
permutations = '\n'.join(' '.join(map(str,permutation)) for permutation in itertools.permutations(list(range(1, n+1))))
return '\n'.join([number_of_permutations, permutations])
def test():
test_string = '3'
result = get_permutations(test_string)
print(result)
assert result == '''\
6
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1'''
print('Test passed!')
def main():
if len(sys.argv) > 1:
filename = sys.argv[1]
with open(filename) as file:
print(get_permutations(file.read()))
else:
test()
if __name__ == '__main__':
main()