Add python and inclusion/exclusion

This commit is contained in:
Oystein Kristoffer Tveit 2021-05-17 22:06:10 +02:00
parent 23750ae959
commit f798bccfcb
10 changed files with 121 additions and 12 deletions

View File

@ -0,0 +1,46 @@
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

View File

@ -0,0 +1,24 @@
import tempfile
import subprocess
import os
import sys
from common import replaceContent
def makeTmpFile(content):
fd, path = tempfile.mkstemp()
with os.fdopen(fd, 'w') as tmp:
tmp.write(content)
return path
def grabOutput(path):
return subprocess.check_output(['python', path]).decode(sys.stdout.encoding)
def processFileContent(content):
path = makeTmpFile(content)
output = grabOutput(path)
return replaceContent(
(path, output),
'Python',
lambda temp, cont: temp.replace('%CODEFILE', cont[0]).replace('%OUTPUT', cont[1])
)

View File

@ -6,6 +6,9 @@ import Graph
import Hasse import Hasse
import Truthtable import Truthtable
import Relations import Relations
import InclusionExclusion
import Python
from common import printerr from common import printerr
def fetchContentType(content): def fetchContentType(content):
@ -26,6 +29,10 @@ def processContent(content):
result = Relations.processFileContent(content) result = Relations.processFileContent(content)
elif contentType == 'Truthtable': elif contentType == 'Truthtable':
result = Truthtable.processFileContent(content) result = Truthtable.processFileContent(content)
elif contentType == 'InclusionExclusion':
result = InclusionExclusion.processFileContent(content)
elif contentType == 'Python':
result = Python.processFileContent(content)
else: else:
printerr('DIDN\'T RECOGNIZE FILE TYPE') printerr('DIDN\'T RECOGNIZE FILE TYPE')
exit(1) exit(1)

View File

@ -3,6 +3,6 @@
Output: Output:
\begin{verbatim} \begin{lstlisting}[breaklines=true, basicstyle=\small]
%OUTPUT %OUTPUT
\end{verbatim} \end{lstlisting}

View File

@ -0,0 +1,6 @@
\begin{align*}
N &- \left[ N(c_1) + N(c_2) + N(c_3) + N(\overline{c_4}) \right] \\
&+ \left[ N(c_1c_2) + N(c_1c_3) + N(c_1\overline{c_4}) + N(c_2c_3) + N(c_2\overline{c_4}) + N(c_3\overline{c_4}) \right] \\
&- \left[ N(c_1c_2c_3) + N(c_1c_2\overline{c_4}) + N(c_1c_3\overline{c_4}) + N(c_2c_3\overline{c_4}) \right] \\
&+ N(c_1c_2c_3\overline{c_4}) \\
\end{align*}

View File

@ -0,0 +1,9 @@
\codeFile{/tmp/tmpuouye9q7}{python}
Output:
\begin{lstlisting}[breaklines=true, basicstyle=\small]
Hello!
\end{lstlisting}

View File

@ -0,0 +1,2 @@
# InclusionExclusion
c_1 c_2 c_3 !c_4

View File

@ -1,2 +1,4 @@
# python # Python
print('Hello!')

View File

@ -1,6 +1,7 @@
\documentclass[12pt]{article} \documentclass[12pt]{article}
\usepackage{ntnu} \usepackage{ntnu}
\usepackage{ntnu-math} \usepackage{ntnu-math}
\usepackage{ntnu-code}
\author{TODO: STUDENTNUMMER} \author{TODO: STUDENTNUMMER}
\title{Exam v2021} \title{Exam v2021}
@ -74,6 +75,10 @@
\verbatimDiagram{hasseDiagramByDivisibility} \verbatimDiagram{hasseDiagramByDivisibility}
\section{Combinatorics}
\verbatimInput{inclusionExclusion}
\section{Graph theory} \section{Graph theory}
\verbatimDiagram{undirectedGraph} \verbatimDiagram{undirectedGraph}
@ -92,4 +97,8 @@
\verbatimDiagram{automata} \verbatimDiagram{automata}
\section{Raw python}
\verbatimInput{python}
\end{document} \end{document}

View File

@ -8,12 +8,16 @@
\contentsline {subsection}{\numberline {3.3}equivalenceDiagram}{5}{subsection.3.3}% \contentsline {subsection}{\numberline {3.3}equivalenceDiagram}{5}{subsection.3.3}%
\contentsline {subsection}{\numberline {3.4}hasse}{6}{subsection.3.4}% \contentsline {subsection}{\numberline {3.4}hasse}{6}{subsection.3.4}%
\contentsline {subsection}{\numberline {3.5}hasseDiagramByDivisibility}{7}{subsection.3.5}% \contentsline {subsection}{\numberline {3.5}hasseDiagramByDivisibility}{7}{subsection.3.5}%
\contentsline {section}{\numberline {4}Graph theory}{8}{section.4}% \contentsline {section}{\numberline {4}Combinatorics}{8}{section.4}%
\contentsline {subsection}{\numberline {4.1}undirectedGraph}{8}{subsection.4.1}% \contentsline {subsection}{\numberline {4.1}inclusionExclusion}{8}{subsection.4.1}%
\contentsline {subsection}{\numberline {4.2}directedGraph}{9}{subsection.4.2}% \contentsline {section}{\numberline {5}Graph theory}{9}{section.5}%
\contentsline {subsection}{\numberline {4.3}complete6}{10}{subsection.4.3}% \contentsline {subsection}{\numberline {5.1}undirectedGraph}{9}{subsection.5.1}%
\contentsline {subsection}{\numberline {4.4}adjacency}{11}{subsection.4.4}% \contentsline {subsection}{\numberline {5.2}directedGraph}{10}{subsection.5.2}%
\contentsline {subsection}{\numberline {4.5}undirectedGraphToMatrix}{12}{subsection.4.5}% \contentsline {subsection}{\numberline {5.3}complete6}{11}{subsection.5.3}%
\contentsline {subsection}{\numberline {4.6}directedFromMatrix}{13}{subsection.4.6}% \contentsline {subsection}{\numberline {5.4}adjacency}{12}{subsection.5.4}%
\contentsline {section}{\numberline {5}Finite state automata}{14}{section.5}% \contentsline {subsection}{\numberline {5.5}undirectedGraphToMatrix}{13}{subsection.5.5}%
\contentsline {subsection}{\numberline {5.1}automata}{14}{subsection.5.1}% \contentsline {subsection}{\numberline {5.6}directedFromMatrix}{14}{subsection.5.6}%
\contentsline {section}{\numberline {6}Finite state automata}{15}{section.6}%
\contentsline {subsection}{\numberline {6.1}automata}{15}{subsection.6.1}%
\contentsline {section}{\numberline {7}Raw python}{16}{section.7}%
\contentsline {subsection}{\numberline {7.1}python}{16}{subsection.7.1}%