Updated the Expression object to include 'wrong_response' and wrote the code needed to start the finding mutations.

This commit is contained in:
2020-04-18 01:13:11 -05:00
parent 58bd836ee2
commit ca47912d05
6 changed files with 54 additions and 37 deletions
+9 -1
View File
@@ -1,12 +1,20 @@
class Expression:
expression = ""
# correct_response = 0
wrong_response = 0
variables = []
def __init__(self, expression_string, variables):
def __init__(self, expression_string, variables, wrong_response):
self.expression = expression_string
self.variables = variables
self.wrong_response = wrong_response
class Variable:
name = ""
type = ""
starting_point = 0
ending_point = 0
VARIABLE_TYPES = {
"i" : "Interest",
"I" : "Interest",
+13 -5
View File
@@ -1,14 +1,20 @@
class Expression:
expression = ""
correct_answer = 0
# correct_response = 0
wrong_response = 0
variables = []
def __init__(self, expression_string, variables, correct):
def __init__(self, expression_string, variables, wrong_response):
self.expression = expression_string
self.variables = variables
self.correct_answer = correct
self.wrong_response = wrong_response
class Variable:
name = ""
type = ""
starting_point = 0
ending_point = 0
VARIABLE_TYPES = {
"i" : "Interest",
"I" : "Interest",
@@ -24,6 +30,8 @@ class Variable:
@staticmethod
def get_variable_type(variable_name):
if not variable_name in Variable.VARIABLE_TYPES:
if len(variable_name) == 0:
return "Unknown"
return Variable.VARIABLE_TYPES[variable_name]
if not variable_name[0] in Variable.VARIABLE_TYPES:
return "Unknown"
return Variable.VARIABLE_TYPES[variable_name[0]]
+9 -16
View File
@@ -1,7 +1,7 @@
import tokenizer
import token
import ctoken
import expression
import json
import jsonpickle
variables = {}
def print_vars(vars):
@@ -55,26 +55,16 @@ def modify_variable(var, vars):
print('Invalid property "%s".' %(response))
print('Commands are "type", "start" and "end".')
def transform(object):
if isinstance(object, expression.Expression) or isinstance(object, expression.Variable):
return object.__dict__
else:
raise TypeError("Only Books and Index will be JSON serialized!")
exp = input("Enter the expression the student will use: ")
correct_answer = input("Enter the correct anwser for the expression: ")
wrong_response = input("Enter the wrong response for the expression: ")
try:
correct_answer = float(correct_answer)
wrong_response = float(wrong_response)
except ValueError:
print("The anwser must be numeric! Will not be included in output file.")
tokens = tokenizer.get_tokens_from_expression_string(exp)
for t in tokens:
if t.type == token.TokenType.variable:
if t.type == ctoken.TokenType.variable:
variables[t.value] = expression.Variable(t.value, expression.Variable.get_variable_type(t.value))
selected_variable = None
@@ -98,4 +88,7 @@ while True:
continue
with open("expression.json", "w", encoding="utf-8") as f:
json.dump(expression.Expression(exp, variables, correct_answer), f, default=transform, indent = 4)
obj = jsonpickle.encode(expression.Expression(exp, variables, wrong_response))
f.write(obj)
f.close()
#json.dump(expression.Expression(exp, variables, correct_answer), f, default=transform, indent = 4)
+1 -1
View File
@@ -1,4 +1,4 @@
import token
import ctoken as token
def get_tokens_from_expression_string(expression_string):
+22 -14
View File
@@ -1,28 +1,36 @@
import token
import ptoken as token
import tokenizer
#import expression
import expression
import calc_engine
#import jsonpickle
import factors
import jsonpickle
base_list = [2, 4, 6, 12]
#fileLocation = input('File Path to JSON: ')
#json = json.loads(open(fileLocation).read())
#userInput = input('Enter your formula: ')
tokens = tokenizer.get_tokens_from_expression_string("5000(2(2(1.08^10)-1)/0.08)")
#with open("expression.json", "r") as f:
# j = json.load(f)
# exp = jsonpickle.decode(f.read())
#exp = expression.Expression.from_json(json.loads(json.load(f)))
# print(exp)
results = calc_engine.calculate_results(tokens.copy())
print(results)
#tokens = tokenizer.replace_variables(tokens, [23, 46, 89])
#tokens = tokenizer.get_tokens_from_expression_string("5000(2(2(1.08^10)-1)/0.08)")
with open("expression.json", "r") as f:
#j = json.load(f)
exp = jsonpickle.decode(f.read())
print(exp)
#results = calc_engine.calculate_results(tokens.copy())
#print(results)
#for t in tokens:
# print("%s (%s) " %(t.value, token.TokenType.get_token_type_name(t.type)), end = "")
#print()
tokens = tokenizer.get_tokens_from_expression_string(exp.expression)
#def factors(factor_object, starting_point, variable_type)
for v in exp.variables:
factor = factors.Factor(v, base_list)
calce_answers(factor, exp.variables[v].starting_point, exp.variables[v].type, exp.wrong_response)
#def calc_answers(factor_object, starting_point, variable_type, wrong_response):