-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathsof-combinatoric.py
More file actions
executable file
·47 lines (36 loc) · 1.44 KB
/
sof-combinatoric.py
File metadata and controls
executable file
·47 lines (36 loc) · 1.44 KB
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
#!/usr/bin/env python3
import itertools
import argparse
parser = argparse.ArgumentParser(description='''
Returns a permutation or combination list:
each combination split by SPACE,
each element split by \',\'
for example:
target: C 4 2
parameter: -t c -n 4 -p 2
output: "0,1 0,2 0,3 1,2 1,3 2,3"
target: P 4 2
parameter: -t p -n 4 -p 2
output: "0,1 0,2 0,3 1,0 1,2 1,3 2,0 2,1 2,3 3,0 3,1 3,2"
''', add_help=True, formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-t', '--type', choices=['c','p'], help='p: permutation; c: combination', default='c')
parser.add_argument('-n', '--number', type=int, help='total number count', required=True)
parser.add_argument('-p', '--pick', type=int, help='pick up count', required=True)
parser.add_argument('-s', '--start', type=int, help='index start value', default=0)
ret_args = vars(parser.parse_args())
if ret_args['number'] < ret_args['pick']:
print(f"Count:{ret_args['pick']} > Number Count:{ret_args['number']} is not allowed")
exit(2)
number = ret_args['number']
pickup = ret_args['pick']
start = ret_args['start']
if start != 0:
number = number + start
if ret_args['type'] == 'c':
result_lst=list(itertools.combinations(range(start, number), pickup))
else:
result_lst=list(itertools.permutations(range(start, number), pickup))
output_str=""
for combine in result_lst:
output_str += " " + ",".join([str(i) for i in combine])
print(output_str.strip())