diff options
author | Peter Wu <peter@lekensteyn.nl> | 2016-12-14 00:46:19 +0100 |
---|---|---|
committer | Peter Wu <peter@lekensteyn.nl> | 2016-12-14 00:46:19 +0100 |
commit | fdae525c81636d257c7caef6ce75ebfec16ebe40 (patch) | |
tree | 940483f3aa8dc2ced9f9ef0a2408876f187efaa0 /integer_sum_from_neighbours/neighborsum-to-latex.py | |
parent | 0162250208e431e2a76ec64ceff4c28a796bf19b (diff) | |
download | 2IMF25-AR-fdae525c81636d257c7caef6ce75ebfec16ebe40.tar.gz |
NeighborSum: finished 4a
Also uncommented questions (a, b, etc.)
Diffstat (limited to 'integer_sum_from_neighbours/neighborsum-to-latex.py')
-rwxr-xr-x | integer_sum_from_neighbours/neighborsum-to-latex.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/integer_sum_from_neighbours/neighborsum-to-latex.py b/integer_sum_from_neighbours/neighborsum-to-latex.py new file mode 100755 index 0000000..a920d9d --- /dev/null +++ b/integer_sum_from_neighbours/neighborsum-to-latex.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +import re +import sys + +# Maximum number of elements +n = 8 + +matrix = [] + +for line in sys.stdin: + line = line.strip() + if line == "unsat": + sys.exit('unsat!') + if line == "sat": + continue + m = re.match(r'a(\d+)_(\d+) -> (\d+)', line) + var_number, step, value = map(int, m.groups()) + + # auto-detect size, extending matrix size if necessary. + while len(matrix) <= step: + matrix.append([None] * n) + + matrix[step][var_number - 1] = value + +# List of cells that have changed since the previous iteration. Note that the +# first row has no changed cell and therefore stores "None" +changedCells = [None] +for i, cells in enumerate(matrix): + if i == 0: + continue # skip first row + previousRow = matrix[i - 1] + for col, prevValue in enumerate(previousRow): + if cells[col] != prevValue: + # Found changed cell! + changedCells.append(col) + break + +line = " " +for col in range(len(matrix[0])): + line += " & $a_%d$" % (col + 1) +line += r" \\" +print(line) +print(r"\hline") + +for step, cells in enumerate(matrix): + line = "%2d" % step + for col, value in enumerate(cells): + if changedCells[step] == col: + line += r" & \bf%2d" % value + else: + line += " & %5d" % value + line += r" \\" + print(line) |