-
Notifications
You must be signed in to change notification settings - Fork 1
/
pcn.py
100 lines (78 loc) · 3.42 KB
/
pcn.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# pcn module is used to read a file and returns a pcn data structure
# islice used to skip the first element of the line
from itertools import islice
# chain used to concatenate the length of the cube and the cube itself
from itertools import chain
# read_pcn reads a file and returns a pcn data structure
def parse(filePath):
"""generates the pcn data structure from a text file
Text file format:
The *first line* of the file is a single positive `int` n: the number of variables.
We number the variables starting with index 1, so if this number was 6,
the variables in your problem are x1, x2, x2, x4, x5, x6.
The second line of the file is a single positive `int` m: number of cubes in this cube list.
If there are 10 cubes in this file, this is a “10”.
Each of the subsequent m lines of the file describes one cube : you have the same number of
lines as the second line of your file. The first number on the line says how many variables
are not don't cares in this cube. If this number is, e.g., 5, then the next 5 numbers on
the line specify the true or complemented form of each variable in this cube. We use a
simple convention: if variable xk appears in true form, then put integer “k” on the line;
if variable xk appears in complement form (~xk) then put integer “-k” on the line
Args:
filePath (`str`): file path of the input .pcn file
Raises:
AssertionError: bad pcn file
Returns:
`tuple(int,list())`: number of variables and the cube list
"""
with open(filePath, "rb") as f:
try:
# read lines from file
lines = iter(f)
numVars = int(next(lines))
cubeCount = int(next(lines))
cubes = [None]*cubeCount
# create the cube list from the file
for i in range(cubeCount):
line = next(lines)
cubes[i] = tuple(islice(map(int, line.split()), 1, None))
return (numVars, tuple(cubes))
except Exception as error:
# throws an exception if the file is not a valid pcn file
raise AssertionError("Bad pcn file {}".format(filePath)) from error
def findNumVars(cubes):
"""returns the number of variables in a cube-list
Args:
cubes (`cube-list`): pcn cube-list data structure
Returns:
`int`: the number of variables in the cube-list
"""
if len(cubes) == 0:
return 0
return max(max(map(abs, cube)) for cube in cubes)
def pcn_to_str(cubes, numVars):
"""converts a pcn data structure to a string
Args:
cubes (`cube-list`): pcn cube-list data structure
Returns:
`str`: string representation of the pcn cube-list data structure
"""
# create the string representation of the pcn data structure
repr = list()
numVars = str(numVars)
repr.append(numVars)
cubes = tuple(set(tuple(sorted(cube, key=abs)) for cube in cubes))
numCubes = str(len(cubes))
repr.append(numCubes)
for cube in cubes:
repr.append(' '.join(map(str, chain((len(cube),), cube))))
return "\n".join(repr)
def write_pcn(filePath, cubes):
"""write the pcn cube-list data structure
Args:
filePath (`str`): out dir path
cubes (`pcn cube-list`): pcn cube-list data structure
"""
# write the pcn data structure to a file
with open(filePath, "w") as f:
f.write(pcn_to_str(cubes))