| | |
| | | from .BerkiParse import BerkiParse |
| | | from .Formatter import Variable |
| | | from .Variable import Variable |
| | | import re |
| | | from math import * |
| | | |
| | | |
| | | class Problem: |
| | | def __init__(self, source=None): |
| | | self.source=source |
| | | self.source.generateVariables() |
| | | self.source.generateSolutions() |
| | | self.problem=self.source.generateProblem() |
| | | |
| | | def toTexFormatterDict(self): |
| | | pass |
| | | |
| | | |
| | | class ProblemSource: |
| | | def __init__(self, parser=None): |
| | | self.introduction = None |
| | | self.subproblems = None |
| | |
| | | self.introduction, self.subproblems, self.parsedVariables, self.parsedSolutions, self.variableGenerator = ( |
| | | parser.get_parsed_sections() |
| | | ) |
| | | self.generateVariables() |
| | | #self.generateVariables() |
| | | |
| | | def generateVariables(self): |
| | | for key in self.parsedVariables: |
| | |
| | | ) |
| | | |
| | | def generateSolutions(self): |
| | | pass |
| | | # a dirty one but it has to be like this ;) |
| | | __retsol = [] |
| | | |
| | | # define variables |
| | | for __varname, __var in self.varDict.items(): |
| | | exec(__varname + "=" + str(__var.get_formatted_value())) |
| | | for __s in self.parsedSolutions: |
| | | __ps = {} |
| | | __ps["correct"] = [] |
| | | __ps["wrong"] = [] |
| | | for __corr in __s["correct"]: |
| | | for __corrsplit in __corr.split(";"): |
| | | if __corrsplit.find("=") >= 0: |
| | | exec(self.substitute_octave(__corrsplit)) |
| | | else: |
| | | __result = eval(self.substitute_octave(__corrsplit)) |
| | | __ps["correct"].append(Variable(__result, formatting=__s["type"])) |
| | | for __corr in __s["wrong"]: |
| | | for __corrsplit in __corr.split(";"): |
| | | if __corrsplit.find("=") >= 0: |
| | | exec(self.substitute_octave(__corrsplit)) |
| | | else: |
| | | __result = eval(self.substitute_octave(__corrsplit)) |
| | | __ps["wrong"].append(Variable(__result, formatting=__s["type"])) |
| | | __ps['glyph']=__s['glyph'] |
| | | __ps['unit']=__s['unit'] |
| | | __retsol.append(__ps) |
| | | return __retsol |
| | | |
| | | def isMultiProblem(self): |
| | | if len(self.subproblems) > 0: |
| | |
| | | text = re.sub(";", "\n", text) |
| | | return text |
| | | |
| | | def solve(self): |
| | | # a dirty one but it has to be like this ;) |
| | | __retsol = [] |
| | | |
| | | # define variables |
| | | for __varname, __var in self.varDict.items(): |
| | | exec(__varname + "=" + str(__var.get_formatted_value())) |
| | | for __s in self.parsedSolutions: |
| | | __ps = {} |
| | | __ps["correct"] = [] |
| | | __ps["wrong"] = [] |
| | | # for __s in self.parsedSolutions: |
| | | for __corr in __s["correct"]: |
| | | for __corrsplit in __corr.split(";"): |
| | | if __corrsplit.find("=") >= 0: |
| | | exec(self.substitute_octave(__corrsplit)) |
| | | else: |
| | | __result = eval(self.substitute_octave(__corrsplit)) |
| | | __ps["correct"].append(Variable(__result, formatting=__s["type"])) |
| | | for __corr in __s["wrong"]: |
| | | for __corrsplit in __corr.split(";"): |
| | | if __corrsplit.find("=") >= 0: |
| | | exec(self.substitute_octave(__corrsplit)) |
| | | else: |
| | | __result = eval(self.substitute_octave(__corrsplit)) |
| | | __ps["wrong"].append(Variable(__result, formatting=__s["type"])) |
| | | __retsol.append(__ps) |
| | | return __retsol |
| | | |
| | | def generate_tex(self, shuffle=True): |
| | | def generateProblem(self, shuffle=True): |
| | | intro = self.substitute_variables(self.introduction) |
| | | sp = [] |
| | | for p in self.subproblems: |
| | | sp.append(self.substitute_variables(p)) |
| | | sol = self.solve() |
| | | sol = self.generateSolutions() |
| | | soltex = [] |
| | | for s in sol: |
| | | entry = { |
| | | "correct": [i.format_as_tex() for i in s["correct"]], |
| | | "wrong": [i.format_as_tex() for i in s["wrong"]], |
| | | "correct": [("${}={}\mathrm{{{}}}$").format(s['glyph'], i.format_as_tex(), s['unit']) for i in s["correct"]], |
| | | "wrong": [("${}={}\mathrm{{{}}}$").format(s['glyph'], i.format_as_tex(), s['unit']) for i in s["wrong"]], |
| | | } |
| | | soltex.append(entry) |
| | | return intro, sp, soltex |