def level_1() -> Equation:
        v = symbols(random.choice(LinearEquations.LETTERS))
        n1 = LinearUtils.random_int(-3, 4)
        n2 = LinearUtils.random_int(-10, 10)

        left = LinearUtils.add_or_subtract(v * n1, n2)
        right = 0
        curr_equation = Equation(left, right, [v], 1300 + random_noise(30))

        if not curr_equation.solvable() or len(
                str(abs(curr_equation.solution()))) > 3:
            return LinearEquations.level_1()

        return curr_equation
    def level_3() -> Equation:
        random_letter = random.choice(LinearEquations.LETTERS)
        v1 = symbols(random_letter)
        n1 = LinearUtils.random_int(-10, 10)
        n2 = LinearUtils.random_int(-3, 4)
        n3 = LinearUtils.random_int(-10, 10)

        left = LinearUtils.add_or_subtract(v1, n1)
        right = LinearUtils.add_or_subtract(n2 * v1, n3)
        curr_equation = Equation(left, right, [v1], 1500 + random_noise(30))

        if not curr_equation.solvable() or len(
                str(abs(curr_equation.solution()))) > 3:
            return LinearEquations.level_3()

        return curr_equation
    def level_8() -> Equation:
        random_letter = random.choice(LinearEquations.LETTERS)
        v = symbols(random_letter)
        n1 = LinearUtils.random_int(-4, 4)
        n2 = LinearUtils.random_int(1, 10)
        n3 = LinearUtils.random_int(-4, 4)

        left = f"({ LinearUtils.add_or_subtract(v, n1) }) ** 2 - { n2 * v }"
        right = f"{ v } * ( {n3} + {v} )"

        curr_equation = Equation(left, right, [v], 2000 + random_noise(30))

        if not curr_equation.solvable() or len(
                str(abs(curr_equation.solution()))) > 4:
            return LinearEquations.level_8()

        return curr_equation
    def level_6() -> Equation:
        random_letter = random.choice(LinearEquations.LETTERS)
        v = symbols(random_letter)
        n1 = LinearUtils.random_strict_int(-6, -2)
        n2 = LinearUtils.random_int(-10, -1)
        n3 = LinearUtils.random_int(2, 10)
        n4 = LinearUtils.random_int(-4, 4)

        left = f"{n1} * ({v - n2})"
        right = f"{n3} * ({LinearUtils.add_or_subtract(n4, v)})"

        curr_equation = Equation(left, right, [v], 1800 + random_noise(30))

        if not curr_equation.solvable() or len(
                str(abs(curr_equation.solution()))) > 4:
            return LinearEquations.level_6()

        return curr_equation
    def level_5() -> Equation:
        random_letter = random.choice(LinearEquations.LETTERS)
        v1 = symbols(random_letter)
        n1 = LinearUtils.random_strict_int(-6, 6)
        n2 = LinearUtils.random_int(-10, 10)
        n3 = LinearUtils.random_int(-5, 5)
        n4 = LinearUtils.random_int(-4, 4)

        left = f"{n1}*({LinearUtils.add_or_subtract(v1, n2)})"
        right = LinearUtils.add_or_subtract(n3 * v1, n4)

        curr_equation = Equation(left, right, [v1], 1700 + random_noise(30))

        if not curr_equation.solvable() or len(
                str(abs(curr_equation.solution()))) > 4:
            return LinearEquations.level_5()

        return curr_equation
    def level_9() -> Equation:
        random_letter = random.choice(LinearEquations.LETTERS)
        v = symbols(random_letter)
        n1 = LinearUtils.random_strict_int(-5, 5)
        n2 = LinearUtils.random_strict_int(-5, 5)
        n3 = LinearUtils.random_int(1, 10)
        n4 = LinearUtils.random_int(-10, 10)
        n5 = LinearUtils.random_int(-5, 5)

        left = f"({ LinearUtils.add_or_subtract(v, n1) }) * ({ LinearUtils.add_or_subtract(v, n2) }) { pm() } {n3}"
        right = f"{ n4 } - { v } * ( {n5} - {v} )"

        curr_equation = Equation(left, right, [v], 2100 + random_noise(30))

        if not curr_equation.solvable() or len(
                str(abs(curr_equation.solution()))) > 4:
            return LinearEquations.level_9()

        return curr_equation
    def level_7() -> Equation:
        random_letter = random.choice(LinearEquations.LETTERS)
        v = symbols(random_letter)
        n1 = LinearUtils.random_strict_int(-6, 6)
        n2 = LinearUtils.random_int(-4, 4)
        n3 = LinearUtils.random_int(-10, 10)
        n4 = LinearUtils.random_int(2, 4)
        n5 = LinearUtils.random_int(-10, 10)
        n6 = LinearUtils.random_int(-4, 4)
        n7 = LinearUtils.random_int(-10, 10)

        left = f"{ n1 } * ({ LinearUtils.add_or_subtract(n2 * v, n3) }) { pm() } { n4 } * ({ LinearUtils.add_or_subtract(v, n5) })"
        right = LinearUtils.add_or_subtract(n6 * v, n7)

        curr_equation = Equation(left, right, [v], 1900 + random_noise(30))

        if not curr_equation.solvable() or len(
                str(abs(curr_equation.solution()))) > 4:
            return LinearEquations.level_7()

        return curr_equation
    def level_11() -> Equation:
        random_letter = random.choice(LinearEquations.LETTERS)
        v = symbols(random_letter)
        n1 = LinearUtils.random_strict_int(-10, 10)
        n2 = LinearUtils.random_int(1, 10)
        n3 = LinearUtils.random_int(-10, 10)
        n4 = LinearUtils.random_int(4, 9)
        n5 = LinearUtils.random_int(-15, 15)
        n6 = LinearUtils.random_int(10, 20)

        left = f"{n1} * {v} - ({v} {pm()} {n2}) * ({n3} - {n4 ** 2 * v})"
        right = f"{n5} + ({n4} * {v} {pm()} {n6}) ** 2"

        curr_equation = Equation(left, right, [v], 2300 + random_noise(30))

        if not curr_equation.solvable() or len(
                str(abs(curr_equation.solution()))) > 4:
            return LinearEquations.level_11()

        return curr_equation
Esempio n. 9
0
 def to_latex(self):
     return "{} = {}".format(LinearUtils.to_latex(str(self.left)), LinearUtils.to_latex(str(self.right)))
Esempio n. 10
0
def check_answer():
    if 'curr_id' not in request.cookies:
        return abort(500)

    data = request.json
    id = request.cookies.get("curr_id")

    cursor.execute(
        "SELECT * FROM users AS u WHERE u.RandomID = '{}' LIMIT 1".format(id))
    user = UserModel.from_tuple(cursor.fetchall()[0])

    cursor.execute("""SELECT * 
                      FROM equations AS e 
                      WHERE e.ID IN (
                          SELECT EquationID 
                          FROM progress AS p 
                          WHERE p.UserID = {} AND p.Correct IS NULL
                      ) LIMIT 1;
                   """.format(user.id))

    curr_eq = cursor.fetchall()

    if not curr_eq:
        return abort(500)

    eq = Equation.from_model(curr_eq[0])

    solution = eq.solution()
    users_attempt = parse_expr(LinearUtils.to_expr(data["value"]),
                               evaluate=True)
    if solution == users_attempt:
        new_rating = Glicko.new_rating(user.rating, user.kfactor, eq.rating,
                                       STANDARD_DEVIATION, Result.Win)
        new_kfactor = Glicko.new_deviation(user.rating, user.kfactor,
                                           eq.rating, STANDARD_DEVIATION)
        correct = True
    else:
        new_rating = Glicko.new_rating(user.rating, user.kfactor, eq.rating,
                                       STANDARD_DEVIATION, Result.Loss)
        new_kfactor = Glicko.new_deviation(user.rating, user.kfactor,
                                           eq.rating, STANDARD_DEVIATION)
        correct = False

    cursor.execute("UPDATE progress "
                   "SET Correct = {}, RatingGain = {}, UserAnswer = '{}' "
                   "WHERE UserID = {} AND EquationID = {}".format(
                       correct, new_rating - user.rating, data["value"],
                       user.id, eq.id))
    db.commit()

    cursor.execute(
        "UPDATE users SET Rating = {}, KFactor = {} WHERE ID = {}".format(
            new_rating, new_kfactor, user.id))
    db.commit()

    new_eq = new_equation(user)
    resp = get_history(request.cookies.get("curr_id"))

    return jsonify({
        "newRating": new_rating,
        "correct": correct,
        "equation": {
            "latex": new_eq.to_latex(),
            "variable": str(new_eq.variables[0]),
            "rating": new_eq.rating
        },
        "history": resp
    })