Add python and inclusion/exclusion
This commit is contained in:
parent
23750ae959
commit
f798bccfcb
|
@ -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
|
||||
|
|
@ -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])
|
||||
)
|
|
@ -6,6 +6,9 @@ import Graph
|
|||
import Hasse
|
||||
import Truthtable
|
||||
import Relations
|
||||
import InclusionExclusion
|
||||
import Python
|
||||
|
||||
from common import printerr
|
||||
|
||||
def fetchContentType(content):
|
||||
|
@ -26,6 +29,10 @@ def processContent(content):
|
|||
result = Relations.processFileContent(content)
|
||||
elif contentType == 'Truthtable':
|
||||
result = Truthtable.processFileContent(content)
|
||||
elif contentType == 'InclusionExclusion':
|
||||
result = InclusionExclusion.processFileContent(content)
|
||||
elif contentType == 'Python':
|
||||
result = Python.processFileContent(content)
|
||||
else:
|
||||
printerr('DIDN\'T RECOGNIZE FILE TYPE')
|
||||
exit(1)
|
||||
|
|
|
@ -3,6 +3,6 @@
|
|||
|
||||
Output:
|
||||
|
||||
\begin{verbatim}
|
||||
\begin{lstlisting}[breaklines=true, basicstyle=\small]
|
||||
%OUTPUT
|
||||
\end{verbatim}
|
||||
\end{lstlisting}
|
|
@ -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*}
|
|
@ -0,0 +1,9 @@
|
|||
|
||||
\codeFile{/tmp/tmpuouye9q7}{python}
|
||||
|
||||
Output:
|
||||
|
||||
\begin{lstlisting}[breaklines=true, basicstyle=\small]
|
||||
Hello!
|
||||
|
||||
\end{lstlisting}
|
|
@ -0,0 +1,2 @@
|
|||
# InclusionExclusion
|
||||
c_1 c_2 c_3 !c_4
|
|
@ -1,2 +1,4 @@
|
|||
# python
|
||||
# Python
|
||||
|
||||
print('Hello!')
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
\documentclass[12pt]{article}
|
||||
\usepackage{ntnu}
|
||||
\usepackage{ntnu-math}
|
||||
\usepackage{ntnu-code}
|
||||
|
||||
\author{TODO: STUDENTNUMMER}
|
||||
\title{Exam v2021}
|
||||
|
@ -74,6 +75,10 @@
|
|||
|
||||
\verbatimDiagram{hasseDiagramByDivisibility}
|
||||
|
||||
\section{Combinatorics}
|
||||
|
||||
\verbatimInput{inclusionExclusion}
|
||||
|
||||
\section{Graph theory}
|
||||
|
||||
\verbatimDiagram{undirectedGraph}
|
||||
|
@ -92,4 +97,8 @@
|
|||
|
||||
\verbatimDiagram{automata}
|
||||
|
||||
\section{Raw python}
|
||||
|
||||
\verbatimInput{python}
|
||||
|
||||
\end{document}
|
|
@ -8,12 +8,16 @@
|
|||
\contentsline {subsection}{\numberline {3.3}equivalenceDiagram}{5}{subsection.3.3}%
|
||||
\contentsline {subsection}{\numberline {3.4}hasse}{6}{subsection.3.4}%
|
||||
\contentsline {subsection}{\numberline {3.5}hasseDiagramByDivisibility}{7}{subsection.3.5}%
|
||||
\contentsline {section}{\numberline {4}Graph theory}{8}{section.4}%
|
||||
\contentsline {subsection}{\numberline {4.1}undirectedGraph}{8}{subsection.4.1}%
|
||||
\contentsline {subsection}{\numberline {4.2}directedGraph}{9}{subsection.4.2}%
|
||||
\contentsline {subsection}{\numberline {4.3}complete6}{10}{subsection.4.3}%
|
||||
\contentsline {subsection}{\numberline {4.4}adjacency}{11}{subsection.4.4}%
|
||||
\contentsline {subsection}{\numberline {4.5}undirectedGraphToMatrix}{12}{subsection.4.5}%
|
||||
\contentsline {subsection}{\numberline {4.6}directedFromMatrix}{13}{subsection.4.6}%
|
||||
\contentsline {section}{\numberline {5}Finite state automata}{14}{section.5}%
|
||||
\contentsline {subsection}{\numberline {5.1}automata}{14}{subsection.5.1}%
|
||||
\contentsline {section}{\numberline {4}Combinatorics}{8}{section.4}%
|
||||
\contentsline {subsection}{\numberline {4.1}inclusionExclusion}{8}{subsection.4.1}%
|
||||
\contentsline {section}{\numberline {5}Graph theory}{9}{section.5}%
|
||||
\contentsline {subsection}{\numberline {5.1}undirectedGraph}{9}{subsection.5.1}%
|
||||
\contentsline {subsection}{\numberline {5.2}directedGraph}{10}{subsection.5.2}%
|
||||
\contentsline {subsection}{\numberline {5.3}complete6}{11}{subsection.5.3}%
|
||||
\contentsline {subsection}{\numberline {5.4}adjacency}{12}{subsection.5.4}%
|
||||
\contentsline {subsection}{\numberline {5.5}undirectedGraphToMatrix}{13}{subsection.5.5}%
|
||||
\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}%
|
||||
|
|
Loading…
Reference in New Issue