Added MathHook
This commit is contained in:
Binary file not shown.
42
main.py
42
main.py
@@ -1,28 +1,44 @@
|
|||||||
#All Rights Reserved John Salguero
|
#All Rights Reserved John Salguero
|
||||||
#Starts the backend to my Youtube stream
|
#Starts the backend to my Youtube stream
|
||||||
|
|
||||||
import requests
|
from problem_generator import generate_problem, normalize
|
||||||
from problem_generator import generate_problem
|
from mathhook import parse, solve
|
||||||
|
from sympy import sympify
|
||||||
# url used to solve the math problems
|
|
||||||
url = "https://api.mathhook.com/solve"
|
|
||||||
|
|
||||||
#define the entry point to the programs
|
#define the entry point to the programs
|
||||||
def main():
|
def main():
|
||||||
problem = generate_problem()
|
problem = generate_problem()
|
||||||
mathhook_payload = {
|
|
||||||
"action": "solve",
|
|
||||||
"expression": problem["problem"],
|
|
||||||
"steps": True
|
|
||||||
}
|
|
||||||
response = requests.post(url, json=mathhook_payload)
|
|
||||||
|
|
||||||
print("Generated Problem:")
|
print("Generated Problem:")
|
||||||
print(problem)
|
print(problem)
|
||||||
print("Solve:")
|
print("Solve:")
|
||||||
print(response.status_code)
|
equation = apply_strategy(problem)
|
||||||
print(response.text)
|
|
||||||
|
|
||||||
|
expr = parse(equation)
|
||||||
|
result = solve(expr, "x")
|
||||||
|
print(result)
|
||||||
|
|
||||||
|
def square_both_sides(problem):
|
||||||
|
lhs, rhs = problem["problem"].split("=")
|
||||||
|
|
||||||
|
lhs = sympify(lhs.strip())
|
||||||
|
rhs = sympify(rhs.strip())
|
||||||
|
|
||||||
|
lhs = lhs ** 2
|
||||||
|
rhs = rhs ** 2
|
||||||
|
|
||||||
|
return f"{normalize(lhs)} = {normalize(rhs)}"
|
||||||
|
|
||||||
|
def factor_or_formula(problem) :
|
||||||
|
return problem["problem"]
|
||||||
|
|
||||||
|
def apply_strategy(problem):
|
||||||
|
if problem["type"] == "radical":
|
||||||
|
return square_both_sides(problem)
|
||||||
|
if problem["type"] == "quadratics":
|
||||||
|
return factor_or_formula(problem)
|
||||||
|
return problem["problem"]
|
||||||
|
|
||||||
#Starts the program
|
#Starts the program
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ def generate_linear():
|
|||||||
x = symbols('x')
|
x = symbols('x')
|
||||||
expr = a * x + b
|
expr = a * x + b
|
||||||
# expanded = n
|
# expanded = n
|
||||||
s = sstr(expr)
|
s = normalize(expr)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"type": "linear",
|
"type": "linear",
|
||||||
@@ -35,7 +35,7 @@ def generate_hidden_factor():
|
|||||||
inner_value = ans + b
|
inner_value = ans + b
|
||||||
right_side = a * inner_value + c * inner_value
|
right_side = a * inner_value + c * inner_value
|
||||||
|
|
||||||
problem = f"{a}({sstr(inner_expr)}) + {c}({sstr(inner_expr)}) = {right_side}"
|
problem = f"{a}({normalize(inner_expr)}) + {c}({normalize(inner_expr)}) = {right_side}"
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"type": "hidden_factor",
|
"type": "hidden_factor",
|
||||||
@@ -55,7 +55,7 @@ def generate_distribution ():
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
"type": "distribution",
|
"type": "distribution",
|
||||||
"problem": f"{a}({sstr(inner_expr)}) = {c}",
|
"problem": f"{a}({normalize(inner_expr)}) = {c}",
|
||||||
"solution": ans
|
"solution": ans
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,7 +73,7 @@ def generate_two_sides ():
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
"type": "two_sides",
|
"type": "two_sides",
|
||||||
"problem": f"{sstr(left_exp)} = {sstr(right_exp)}",
|
"problem": f"{normalize(left_exp)} = {normalize(right_exp)}",
|
||||||
"solution": ans
|
"solution": ans
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,7 +90,7 @@ def generate_like_terms ():
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
"type": "like_terms",
|
"type": "like_terms",
|
||||||
"problem": f"{sstr(expr)} = {d}",
|
"problem": f"{normalize(expr)} = {d}",
|
||||||
"solution": ans
|
"solution": ans
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,7 +111,7 @@ def generate_quadratic ():
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
"type": "quadratic",
|
"type": "quadratic",
|
||||||
"problem": f"{sstr(expr)} = 0",
|
"problem": f"{normalize(expr)} = 0",
|
||||||
"solution": solution
|
"solution": solution
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +130,7 @@ def generate_difference_squares ():
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
"type": "difference_squares",
|
"type": "difference_squares",
|
||||||
"problem": f"{sstr(expr)} = 0",
|
"problem": f"{normalize(expr)} = 0",
|
||||||
"solution": solution
|
"solution": solution
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,7 +144,7 @@ def generate_zero_product ():
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
"type": "zero_product",
|
"type": "zero_product",
|
||||||
"problem": f"{sstr(expr)} = 0",
|
"problem": f"{normalize(expr)} = 0",
|
||||||
"solution": [-a, -b]
|
"solution": [-a, -b]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,7 +159,7 @@ def generate_radical ():
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
"type": "radical",
|
"type": "radical",
|
||||||
"problem": f"{sstr(expr)} = {b}",
|
"problem": f"{normalize(expr)} = {b}",
|
||||||
"solution": ans
|
"solution": ans
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,7 +175,7 @@ def generate_fraction ():
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
"type": "fraction",
|
"type": "fraction",
|
||||||
"problem": f"{sstr(expr)} = {c}",
|
"problem": f"{normalize(expr)} = {c}",
|
||||||
"solution": ans
|
"solution": ans
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,7 +197,7 @@ def generate_binomial ():
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
"type": "binomial",
|
"type": "binomial",
|
||||||
"problem": f"{sstr(expr)} = {e}",
|
"problem": f"{normalize(expr)} = {e}",
|
||||||
"solution": ans
|
"solution": ans
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -214,7 +214,7 @@ def generate_tricky ():
|
|||||||
expanded = expanded - expr
|
expanded = expanded - expr
|
||||||
expanded = expanded / (x - r1)
|
expanded = expanded / (x - r1)
|
||||||
# expanded = n
|
# expanded = n
|
||||||
s = sstr(expanded)
|
s = normalize(expanded)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"type": "tricky",
|
"type": "tricky",
|
||||||
@@ -222,6 +222,9 @@ def generate_tricky ():
|
|||||||
"solution": r2
|
"solution": r2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def normalize(expr):
|
||||||
|
return sstr(expr).replace("**", "^")
|
||||||
|
|
||||||
def generate_problem():
|
def generate_problem():
|
||||||
template = random.choice(TEMPLATES)
|
template = random.choice(TEMPLATES)
|
||||||
return template()
|
return template()
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
sympy
|
sympy
|
||||||
requests
|
mathhook
|
||||||
Reference in New Issue
Block a user