-
Notifications
You must be signed in to change notification settings - Fork 41
/
e021-sign.py
executable file
·67 lines (52 loc) · 1.46 KB
/
e021-sign.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
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
# coding=utf-8
# Enumerating Oriented Gene Orderings
# ===================================
#
# A signed permutation of length n is some ordering of the positive integers
# {1,2,…,n} in which each integer is then provided with either a positive or
# negative sign (for the sake of simplicity, we omit the positive sign). For
# example, π=(5,−3,−2,1,4) is a signed permutation of length 5.
#
# 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).
#
# Sample Dataset
# --------------
# 2
#
# Sample Output
# -------------
# 8
# -1 -2
# -1 2
# 1 -2
# 1 2
# -2 -1
# -2 1
# 2 -1
# 2 1
from itertools import permutations, product
def merge_product(product):
result = []
numbers, signs = product
for i, number in enumerate(numbers):
sign = signs[i]
number = int(sign + str(number))
result.append(number)
return result
def result(n):
numbers = list(permutations(range(1, n + 1)))
signs = list(product('-+', repeat=n))
results = list(product(numbers, signs))
results = map(merge_product, results)
return results
if __name__ == "__main__":
small_dataset = 2
large_dataset = int(open('datasets/rosalind_sign.txt').read().strip())
results = result(large_dataset)
print len(results)
for r in results:
print ' '.join(map(str, r))