From f798bccfcb8fab2721f3d7d5a6d20c12cbe52f04 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Mon, 17 May 2021 22:06:10 +0200 Subject: [PATCH] Add python and inclusion/exclusion --- exam_template/python/InclusionExclusion.py | 46 +++++++++++++++++++ exam_template/python/Python.py | 24 ++++++++++ exam_template/python/run.py | 7 +++ exam_template/python/tex_templates/Python.tex | 4 +- .../graphics/inclusionExclusion.tex | 6 +++ exam_template_graphics/graphics/python.tex | 9 ++++ .../graphics/src/inclusionExclusion.txt | 2 + .../graphics/src/python.txt | 4 +- exam_template_graphics/main.tex | 9 ++++ exam_template_graphics/main.toc | 22 +++++---- 10 files changed, 121 insertions(+), 12 deletions(-) create mode 100644 exam_template/python/InclusionExclusion.py create mode 100644 exam_template_graphics/graphics/inclusionExclusion.tex create mode 100644 exam_template_graphics/graphics/python.tex create mode 100644 exam_template_graphics/graphics/src/inclusionExclusion.txt diff --git a/exam_template/python/InclusionExclusion.py b/exam_template/python/InclusionExclusion.py new file mode 100644 index 0000000..b8ee340 --- /dev/null +++ b/exam_template/python/InclusionExclusion.py @@ -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 + \ No newline at end of file diff --git a/exam_template/python/Python.py b/exam_template/python/Python.py index e69de29..4ea595d 100644 --- a/exam_template/python/Python.py +++ b/exam_template/python/Python.py @@ -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]) + ) diff --git a/exam_template/python/run.py b/exam_template/python/run.py index 3f5ef7d..8a332c8 100644 --- a/exam_template/python/run.py +++ b/exam_template/python/run.py @@ -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) diff --git a/exam_template/python/tex_templates/Python.tex b/exam_template/python/tex_templates/Python.tex index 37f07af..6a9fdc1 100644 --- a/exam_template/python/tex_templates/Python.tex +++ b/exam_template/python/tex_templates/Python.tex @@ -3,6 +3,6 @@ Output: -\begin{verbatim} +\begin{lstlisting}[breaklines=true, basicstyle=\small] %OUTPUT -\end{verbatim} \ No newline at end of file +\end{lstlisting} \ No newline at end of file diff --git a/exam_template_graphics/graphics/inclusionExclusion.tex b/exam_template_graphics/graphics/inclusionExclusion.tex new file mode 100644 index 0000000..4e44a34 --- /dev/null +++ b/exam_template_graphics/graphics/inclusionExclusion.tex @@ -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*} \ No newline at end of file diff --git a/exam_template_graphics/graphics/python.tex b/exam_template_graphics/graphics/python.tex new file mode 100644 index 0000000..a767e00 --- /dev/null +++ b/exam_template_graphics/graphics/python.tex @@ -0,0 +1,9 @@ + +\codeFile{/tmp/tmpuouye9q7}{python} + +Output: + +\begin{lstlisting}[breaklines=true, basicstyle=\small] +Hello! + +\end{lstlisting} \ No newline at end of file diff --git a/exam_template_graphics/graphics/src/inclusionExclusion.txt b/exam_template_graphics/graphics/src/inclusionExclusion.txt new file mode 100644 index 0000000..78b53a5 --- /dev/null +++ b/exam_template_graphics/graphics/src/inclusionExclusion.txt @@ -0,0 +1,2 @@ +# InclusionExclusion +c_1 c_2 c_3 !c_4 \ No newline at end of file diff --git a/exam_template_graphics/graphics/src/python.txt b/exam_template_graphics/graphics/src/python.txt index 6376748..43eeaed 100644 --- a/exam_template_graphics/graphics/src/python.txt +++ b/exam_template_graphics/graphics/src/python.txt @@ -1,2 +1,4 @@ -# python +# Python + +print('Hello!') diff --git a/exam_template_graphics/main.tex b/exam_template_graphics/main.tex index 7886141..b138920 100644 --- a/exam_template_graphics/main.tex +++ b/exam_template_graphics/main.tex @@ -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} \ No newline at end of file diff --git a/exam_template_graphics/main.toc b/exam_template_graphics/main.toc index a96543f..1cd7947 100644 --- a/exam_template_graphics/main.toc +++ b/exam_template_graphics/main.toc @@ -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}%