Added MathHook
This commit is contained in:
42
main.py
42
main.py
@@ -1,28 +1,44 @@
|
||||
#All Rights Reserved John Salguero
|
||||
#Starts the backend to my Youtube stream
|
||||
|
||||
import requests
|
||||
from problem_generator import generate_problem
|
||||
|
||||
# url used to solve the math problems
|
||||
url = "https://api.mathhook.com/solve"
|
||||
from problem_generator import generate_problem, normalize
|
||||
from mathhook import parse, solve
|
||||
from sympy import sympify
|
||||
|
||||
#define the entry point to the programs
|
||||
def main():
|
||||
problem = generate_problem()
|
||||
mathhook_payload = {
|
||||
"action": "solve",
|
||||
"expression": problem["problem"],
|
||||
"steps": True
|
||||
}
|
||||
response = requests.post(url, json=mathhook_payload)
|
||||
|
||||
|
||||
print("Generated Problem:")
|
||||
print(problem)
|
||||
print("Solve:")
|
||||
print(response.status_code)
|
||||
print(response.text)
|
||||
equation = apply_strategy(problem)
|
||||
|
||||
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
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user