예제 #1
0
def cheat_check(user_answer, disallowed, variables):
    """Checks whether the user has used symbols/functions that are not allowed"""
    standard_disallowed = ['int', 'test', "'", '@', '?']
    for x in disallowed:
        replaced_x = replace_variables_from_array(variables, x)
        standard_disallowed.append(parse_solution(replaced_x, ''))

    for s in standard_disallowed:
        if str(s).replace(' ', '') in str(user_answer):
            return True
    return False
예제 #2
0
def required_check(user_answer, required, variables):
    """Checks if the user answer meets the requirements set by the teacher"""
    return_value = False
    for x in range(0, len(required)):
        required[x] = replace_variables_from_array(variables, required[x])
    for s in required:
        try:
            t_s = parse_solution(s, '')
            t_s = parenthesis_removal(t_s)
            if str(t_s).replace(' ', '') in str(user_answer):
                break
            #elif parse_using_sympy(latex_to_sympy(t_s) + '+0' + '==' + latex_to_sympy(user_answer) + '+0'):
            #    break
            return_value = True

        except Exception as e:
            print('in exception ')
            print(e)
            if str(s) not in str(user_answer):
                return_value = True
                break
    return return_value
예제 #3
0
def make_answer_context_dict(form_values):
    """Returns context dict for use on the answer page"""
    user_answer = form_values['user_answer']
    user_answer = user_answer.replace(',','.')
    user_answer = user_answer.replace('..', ',')  # A cheeky way to circumvent removal of ','
    template_type = form_values['template_type']
    template_specific = form_values['template_specific']
    q = Template.objects.get(pk=form_values['primary_key'])
    variable_dictionary = form_values['variable_dictionary'].split('§')
    replacing_words = form_values['replacing_words']
    random_domain = q.random_domain

    if template_type != 'blanks':
        answer = q.answer.replace('\\\\', '\\')
        answer = answer.replace('(', '+parenthesisleft+')  # Done to preserve original parenthesis
        answer = answer.replace(')', '+parenthesisright+')  # Done to preserve original parenthesis
        answer = replace_variables_from_array(variable_dictionary, answer)
        #answer = add_phantom_minus(answer)
    else:
        #answer = get_values_from_position(template_specific, q.solution.replace('\\\\', '\\'))
        answer = get_fillin_answers(q.fill_in.replace('\\\\', '\\'))
        answer = answer.replace('(', '+parenthesisleft+')  # Done to preserve original parenthesis
        answer = answer.replace(')', '+parenthesisright+')  # Done to preserve original parenthesis
        answer = replace_variables_from_array(variable_dictionary, answer)

    original_user_answer = user_answer.replace('§', '\\text{ og }')
    #answer = remove_pm_and_add_parenthesis(answer) # Replace with new parenthesis parsing
    answer = parse_answer(answer, random_domain)
    answer = parenthesis_removal(answer)
    answer = answer.replace('`', '')
    answer = answer.split('§')
    solution = str(q.question_text.replace('\\\\', '\\')) + "§" + str(q.solution.replace('\\\\', '\\'))
    #solution = add_phantom_minus(solution)
    solution = solution.replace('(', '+parenthesisleft+')  # Done to preserve original parenthesis
    solution = solution.replace(')', '+parenthesisright+')  # Done to preserve original parenthesis
    solution = replace_variables_from_array(variable_dictionary, solution)
    #solution = remove_pm_and_add_parenthesis(solution)
    solution = parse_solution(solution, random_domain)
    solution = parenthesis_removal(solution)
    if len(replacing_words) > 0:
        solution = replace_words(solution, replacing_words)['sentence']
    user_answer = user_answer.split('§')  # If a string doesn't contain the character it returns a list with 1 element
    # We format both the user answer and the answer the same way.
    user_answer = [after_equal_sign(x) for x in user_answer]  # Only get the values after last equal sign.
    #user_answer = calculate_array(user_answer, random_domain)
    answer = [after_equal_sign(x) for x in answer]
    #answer = calculate_array(answer, random_domain)
    answer_text = "\\text{Du har svart }" + original_user_answer + \
                      "\\text{. Det er feil. Svaret er: }" + '\\text{ og }'.join(answer)

    correct_answer = check_answer(user_answer, answer, template_type, q.margin_of_error)  # Check if the user answered correctly.

    if correct_answer:
        answer_text = "\\text{Du har svart riktig!}"

    answer_text = latex_exceptions(answer_text)

    graph = q.graph
    if graph:
        graph = json.loads(graph)

    for x in range(0, len(graph)):
            graph[x] = replace_variables_from_array(variable_dictionary, graph[x])
            graph[x] = parse_solution(graph[x], q.random_domain)

    if graph != None and graph != '':  # to prevent error if none
        graph = json.dumps(graph)
    context_dict = {'title': "Oppgavegen", 'answer': str(answer_text),
                    'solution': solution, 'user_won': correct_answer, 'graph': graph,
                    'graph_color' : q.graph_color, 'graph_settings': q.graph_settings}
    return context_dict