60 lines
1.7 KiB
Python
60 lines
1.7 KiB
Python
#All Rights Reserved John Salguero
|
|
#Starts the backend to my Youtube stream
|
|
|
|
from problem_generator import generate_problem
|
|
from steps_generator import generate_steps
|
|
from sympy import init_printing, sympify
|
|
import time
|
|
import json
|
|
|
|
#define the entry point to the programs
|
|
def main():
|
|
init_printing(order='lex')
|
|
count = -1
|
|
no_problem = True
|
|
|
|
problem = generate_problem()
|
|
problem["steps"] = generate_steps(problem);
|
|
|
|
|
|
no_problem = check_solution(problem["steps"][-1]["after"], problem["solution"])
|
|
if no_problem:
|
|
with open("www/data/data.json", "w") as f:
|
|
json.dump(problem, f)
|
|
|
|
def check_solution(got, solution):
|
|
values = set([sympify(r.split("=")[1].strip()) for r in got.split(",")])
|
|
if is_iterable(solution) and not isinstance(solution, str):
|
|
solutions = set([sympify(r) for r in solution])
|
|
else:
|
|
solutions_list = []
|
|
solutions_list.append(sympify(solution))
|
|
solutions = set(solutions_list)
|
|
return solutions == values
|
|
|
|
|
|
def pretty_print_steps(steps):
|
|
print("\n" + "=" * 50)
|
|
|
|
for i, step in enumerate(steps, start=1):
|
|
print(f"\nStep {i}")
|
|
print("-" * 50)
|
|
print(f"Before: {step.get('before', '')}")
|
|
print(f"After: {step.get('after', '')}")
|
|
|
|
for key in step:
|
|
if key not in ("before", "after"):
|
|
print(f"{key.capitalize()}: {step[key]}")
|
|
|
|
print("\n" + "=" * 50)
|
|
|
|
def is_iterable(obj):
|
|
try:
|
|
iter(obj)
|
|
return True
|
|
except TypeError:
|
|
return False
|
|
|
|
#Starts the program
|
|
if __name__ == "__main__":
|
|
main() |