First Rendition of Problem Generator
This commit is contained in:
BIN
__pycache__/problem_generator.cpython-313.pyc
Normal file
BIN
__pycache__/problem_generator.cpython-313.pyc
Normal file
Binary file not shown.
29
main.py
Normal file
29
main.py
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#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"
|
||||||
|
|
||||||
|
#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)
|
||||||
|
|
||||||
|
|
||||||
|
#Starts the program
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
242
problem_generator.py
Normal file
242
problem_generator.py
Normal file
@@ -0,0 +1,242 @@
|
|||||||
|
#All Rights Reserved John Salguero
|
||||||
|
#Generates Problems from a list of problems
|
||||||
|
|
||||||
|
import random
|
||||||
|
from sympy import *
|
||||||
|
|
||||||
|
def generate_linear():
|
||||||
|
#ax + b = c
|
||||||
|
a = random.choice([i for i in range(-10, 16) if i != 0])
|
||||||
|
ans = random.choice([i for i in range(-10, 11)])
|
||||||
|
b = random.choice([i for i in range(-10, 11)])
|
||||||
|
|
||||||
|
c = a * ans + b
|
||||||
|
x = symbols('x')
|
||||||
|
expr = a * x + b
|
||||||
|
# expanded = n
|
||||||
|
s = sstr(expr)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"type": "linear",
|
||||||
|
"problem": f"{s} = {c}",
|
||||||
|
"solution": ans
|
||||||
|
}
|
||||||
|
|
||||||
|
def generate_hidden_factor():
|
||||||
|
#a(x + b) + c(x + b) = d
|
||||||
|
ans = random.choice([i for i in range(-10, 16)])
|
||||||
|
b = random.choice([i for i in range(-5, 6) if i != 0])
|
||||||
|
|
||||||
|
a = random.choice([i for i in range(-5, 6) if i != 0])
|
||||||
|
c = random.choice([i for i in range(-5, 6) if i != 0])
|
||||||
|
|
||||||
|
x = symbols('x')
|
||||||
|
inner_expr = x + b
|
||||||
|
inner_value = ans + b
|
||||||
|
right_side = a * inner_value + c * inner_value
|
||||||
|
|
||||||
|
problem = f"{a}({sstr(inner_expr)}) + {c}({sstr(inner_expr)}) = {right_side}"
|
||||||
|
|
||||||
|
return {
|
||||||
|
"type": "hidden_factor",
|
||||||
|
"problem": problem,
|
||||||
|
"solution": ans
|
||||||
|
}
|
||||||
|
|
||||||
|
def generate_distribution ():
|
||||||
|
#a(x + b) = c
|
||||||
|
ans = random.choice([i for i in range(-10, 16)])
|
||||||
|
a = random.choice([i for i in range(-5, 6) if i not in (0, 1, -1)])
|
||||||
|
b = random.choice([i for i in range(-5, 6) if i != 0])
|
||||||
|
|
||||||
|
c = a * (ans + b)
|
||||||
|
x = symbols('x')
|
||||||
|
inner_expr = x + b
|
||||||
|
|
||||||
|
return {
|
||||||
|
"type": "distribution",
|
||||||
|
"problem": f"{a}({sstr(inner_expr)}) = {c}",
|
||||||
|
"solution": ans
|
||||||
|
}
|
||||||
|
|
||||||
|
def generate_two_sides ():
|
||||||
|
#ax + b = dx + e : a != d
|
||||||
|
ans = random.choice([i for i in range(-10, 16)])
|
||||||
|
a = random.choice([i for i in range(-5, 6) if i != 0])
|
||||||
|
b = random.choice([i for i in range(-5, 6) if i != 0])
|
||||||
|
d = random.choice([i for i in range(-5, 6) if i != a and i != 0])
|
||||||
|
e = a * ans + b - d * ans
|
||||||
|
|
||||||
|
x = symbols('x')
|
||||||
|
left_exp = a * x + b
|
||||||
|
right_exp = d * x + e
|
||||||
|
|
||||||
|
return {
|
||||||
|
"type": "two_sides",
|
||||||
|
"problem": f"{sstr(left_exp)} = {sstr(right_exp)}",
|
||||||
|
"solution": ans
|
||||||
|
}
|
||||||
|
|
||||||
|
def generate_like_terms ():
|
||||||
|
#ax + bx + c = d
|
||||||
|
ans = random.choice([i for i in range(-10, 16)])
|
||||||
|
a = random.choice([i for i in range(-5, 6) if i != 0])
|
||||||
|
b = random.choice([i for i in range(-5, 6) if i != 0 and i != -a])
|
||||||
|
c = random.choice([i for i in range(-10, 16) if i != 0])
|
||||||
|
d = a * ans + b * ans + c
|
||||||
|
|
||||||
|
x = symbols('x')
|
||||||
|
expr = Add(a*x, b*x, c, evaluate=False)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"type": "like_terms",
|
||||||
|
"problem": f"{sstr(expr)} = {d}",
|
||||||
|
"solution": ans
|
||||||
|
}
|
||||||
|
|
||||||
|
def generate_quadratic ():
|
||||||
|
#ax² + bx + c = 0
|
||||||
|
r1 = random.choice([i for i in range(-10, 16)])
|
||||||
|
r2 = random.choice([i for i in range(-10, 16)])
|
||||||
|
n = random.choice([i for i in range(-5, 6) if i != 0])
|
||||||
|
s = random.choice([i for i in range(-5, 6) if i != 0])
|
||||||
|
|
||||||
|
x = symbols('x')
|
||||||
|
expr = n *(x - r1) * (x - r2)
|
||||||
|
expr = simplify(expr)
|
||||||
|
if r1 == r2:
|
||||||
|
solution = r1
|
||||||
|
else:
|
||||||
|
solution = [r1, r2]
|
||||||
|
|
||||||
|
return {
|
||||||
|
"type": "quadratic",
|
||||||
|
"problem": f"{sstr(expr)} = 0",
|
||||||
|
"solution": solution
|
||||||
|
}
|
||||||
|
|
||||||
|
def generate_difference_squares ():
|
||||||
|
#x² - a² = 0
|
||||||
|
ans = random.choice([i for i in range(0, 13)])
|
||||||
|
a = ans
|
||||||
|
|
||||||
|
x = symbols('x')
|
||||||
|
expr = Add(x**2, -a**2)
|
||||||
|
|
||||||
|
if ans !=0:
|
||||||
|
solution = [ans, -ans]
|
||||||
|
else:
|
||||||
|
solution = ans
|
||||||
|
|
||||||
|
return {
|
||||||
|
"type": "difference_squares",
|
||||||
|
"problem": f"{sstr(expr)} = 0",
|
||||||
|
"solution": solution
|
||||||
|
}
|
||||||
|
|
||||||
|
def generate_zero_product ():
|
||||||
|
#(x + a)(x + b) = 0
|
||||||
|
a = random.choice([i for i in range(-5, 6)])
|
||||||
|
b = random.choice([i for i in range(-5, 6) if i != a])
|
||||||
|
|
||||||
|
x = symbols('x')
|
||||||
|
expr = Mul(x+a, x+b, evaluate=False)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"type": "zero_product",
|
||||||
|
"problem": f"{sstr(expr)} = 0",
|
||||||
|
"solution": [-a, -b]
|
||||||
|
}
|
||||||
|
|
||||||
|
def generate_radical ():
|
||||||
|
#√(x + a) = b
|
||||||
|
a = random.choice([i for i in range(-10, 16)])
|
||||||
|
b = random.choice([i for i in range(1, 12)])
|
||||||
|
ans = b**2 - a
|
||||||
|
|
||||||
|
x = symbols('x')
|
||||||
|
expr = root(x+a, 2)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"type": "radical",
|
||||||
|
"problem": f"{sstr(expr)} = {b}",
|
||||||
|
"solution": ans
|
||||||
|
}
|
||||||
|
|
||||||
|
def generate_fraction ():
|
||||||
|
#(x/a) + b = c
|
||||||
|
a = random.choice([i for i in range(-7, 8) if i != 0 and i != 1])
|
||||||
|
b = random.choice([i for i in range(-15, 16)])
|
||||||
|
c = random.choice([i for i in range(-7, 8)])
|
||||||
|
ans = (c - b) * a
|
||||||
|
|
||||||
|
x = symbols('x')
|
||||||
|
expr = x / a + b
|
||||||
|
|
||||||
|
return {
|
||||||
|
"type": "fraction",
|
||||||
|
"problem": f"{sstr(expr)} = {c}",
|
||||||
|
"solution": ans
|
||||||
|
}
|
||||||
|
|
||||||
|
def generate_binomial ():
|
||||||
|
#a(x + b) + c(x + d) = e
|
||||||
|
ans = random.choice([i for i in range(-15, 16)])
|
||||||
|
a = random.choice([i for i in range(-5, 6) if i != 0])
|
||||||
|
b = random.choice([i for i in range(-5, 6)])
|
||||||
|
|
||||||
|
c = random.choice([i for i in range(-5, 6) if i != 0 and i != -a])
|
||||||
|
d = random.choice([i for i in range(-5, 6)])
|
||||||
|
|
||||||
|
e = a * (ans + b) + c * (ans + d)
|
||||||
|
|
||||||
|
x = symbols('x')
|
||||||
|
left_expr = Mul(a, x + b, evaluate=False)
|
||||||
|
right_expr = Mul(c, x + d, evaluate=False)
|
||||||
|
expr = Add(left_expr, right_expr, evaluate=False)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"type": "binomial",
|
||||||
|
"problem": f"{sstr(expr)} = {e}",
|
||||||
|
"solution": ans
|
||||||
|
}
|
||||||
|
|
||||||
|
def generate_tricky ():
|
||||||
|
#generate random numbers
|
||||||
|
n = random.choice([i for i in range(-5, 6) if i != 0])
|
||||||
|
r1 = random.choice([i for i in range(-10, 16)])
|
||||||
|
r2 = random.choice([i for i in range(-10, 16) if i != r1])
|
||||||
|
|
||||||
|
x = symbols('x')
|
||||||
|
expr = (x - r1) * (x - r2)
|
||||||
|
expanded = expand(expr)
|
||||||
|
expr = n * (x - r1)
|
||||||
|
expanded = expanded - expr
|
||||||
|
expanded = expanded / (x - r1)
|
||||||
|
# expanded = n
|
||||||
|
s = sstr(expanded)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"type": "tricky",
|
||||||
|
"problem": f"{s} = {-n}",
|
||||||
|
"solution": r2
|
||||||
|
}
|
||||||
|
|
||||||
|
def generate_problem():
|
||||||
|
template = random.choice(TEMPLATES)
|
||||||
|
return template()
|
||||||
|
|
||||||
|
TEMPLATES = [
|
||||||
|
generate_linear,
|
||||||
|
generate_hidden_factor,
|
||||||
|
generate_distribution,
|
||||||
|
generate_two_sides,
|
||||||
|
generate_like_terms,
|
||||||
|
generate_quadratic,
|
||||||
|
generate_difference_squares,
|
||||||
|
generate_zero_product,
|
||||||
|
generate_radical,
|
||||||
|
generate_fraction,
|
||||||
|
generate_binomial,
|
||||||
|
generate_tricky
|
||||||
|
]
|
||||||
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
sympy
|
||||||
|
requests
|
||||||
Reference in New Issue
Block a user