MA0301/exam/python/InclusionExclusion.py

46 lines
1.1 KiB
Python

from itertools import combinations
def latexifyCondition(condition):
return condition[0] if condition[1] else f'\\overline{{{condition[0]}}}'
def latexify(listOfConditions): # [(str,bool)]
lines = []
for n in range(1, len(listOfConditions) + 1):
line = []
for x in combinations(listOfConditions, n):
line.append(''.join(latexifyCondition(c) for c in x))
lines.append(line)
linestrs = []
for line in lines:
linestr = ' + '.join(f'N({x})' for x in line)
if len(line) > 1:
linestr = f'\\left[ {linestr} \\right]'
linestrs.append(linestr)
b = False
result = '\\begin{align*}\nN'
for l in linestrs:
result += ' &+ ' if b else ' &- '
result += l + ' \\\\\n'
b = not b
result += '\\end{align*}'
return result
def parseInput(string):
result = []
for x in string.split(' '):
if x.startswith('!'):
result.append((x[1:], False))
else:
result.append((x, True))
return result
def processFileContent(raw):
listOfConditions = parseInput(raw)
content = latexify(listOfConditions)
return content
if __name__ == '__main__':
pass