-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix.py
executable file
·84 lines (63 loc) · 1.91 KB
/
fix.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
import argparse, sys
import re
parser = argparse.ArgumentParser(description='Fix variables in system.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-f', dest='fix', required=True,
help='String indicating how to fix variables, e.g., "01" fixes x_(n-1)=0 and x_n=1 (for n variables in total).')
args = parser.parse_args()
fix = args.fix
fix = fix.strip()
fix = fix.replace(" ", "")
fix = fix.replace(",", "")
fix = fix.replace(";", "")
fix = [int(v) for v in fix]
try:
for data in sys.stdin:
data = data.strip()
if len(data) == 0:
print(data)
continue
if not (data[0] == '0' or data[0] =='1'):
if data.startswith("Number of variables"):
m = re.match(r'[^0-9]*([0-9]*)', data)
n = int(m.group(1))
print("Number of variables (n) : {0}".format(n-len(fix)))
else:
print(data)
continue
data = data.replace(" ", "")
data = data.replace(";", "")
if not (((n*(n-1)) >> 1) + 2*n + 1 == len(data)):
sys.stderr.write("Input length error!")
sys.exit(-1)
data = [int(v) for v in data]
new_n = n-len(fix)
sq = []
lin = [0] * new_n
const = 0
for i in range(n):
for j in range(i+1):
val = data[0]
data = data[1:]
if i >= new_n:
if j >= new_n:
const ^= val & fix[i-new_n] & fix[j-new_n]
else:
lin[j] ^= val & fix[i-new_n]
else:
if j >= new_n:
lin[i] ^= val & fix[j-new_n]
else:
sq.append(val)
for i in range(n):
val = data[0]
data = data[1:]
if i >= new_n:
const ^= val & fix[i-new_n]
else:
lin[i] ^= val
const ^= data[0]
print(" ".join([str(v) for v in (sq + lin + [const])]) + ";")
except BrokenPipeError:
pass