From 209bfb7978ed37718a09d6f9c77e45f3a11d1a7d Mon Sep 17 00:00:00 2001 From: Priyansh Anand Date: Fri, 16 Oct 2020 15:57:23 +0530 Subject: [PATCH] Added linearEquations generator --- mathgenerator/mathgen.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/mathgenerator/mathgen.py b/mathgenerator/mathgen.py index 88b05b9..247fab8 100644 --- a/mathgenerator/mathgen.py +++ b/mathgenerator/mathgen.py @@ -276,6 +276,29 @@ def factoringFunc(range_x1 = 10, range_x2 = 10): solution = f"(x{x1})(x{x2})" return problem, solution +def linearEquationsFunc(n = 2, varRange = 20, coeffRange = 20): + if n > 10: + print("[!] n cannot be greater than 10") + return None, None + + vars = ['x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g'][:n] + soln = [ random.randint(-varRange, varRange) for i in range(n) ] + + problem = list() + solution = ", ".join(["{} = {}".format(vars[i], soln[i]) for i in range(n)]) + for _ in range(n): + coeff = [ random.randint(-coeffRange, coeffRange) for i in range(n) ] + res = sum([ coeff[i] * soln[i] for i in range(n)]) + + prob = ["{}{}".format(coeff[i], vars[i]) if coeff[i] != 0 else "" for i in range(n)] + while "" in prob: + prob.remove("") + prob = " + ".join(prob) + " = " + str(res) + problem.append(prob) + + problem = "\n".join(problem) + return problem, solution + # || Class Instances #Format is: @@ -301,4 +324,5 @@ intMatrix22Multiplication = Generator("Integer Multiplication with 2x2 Matrix", areaOfTriangle = Generator("Area of Triangle", 18, "Area of Triangle with side lengths a, b, c = ", "area", areaOfTriangleFunc) doesTriangleExist = Generator("Triangle exists check", 19, "Does triangle with sides a, b and c exist?","Yes/No", isTriangleValidFunc) midPointOfTwoPoint=Generator("Midpoint of the two point", 20,"((X1,Y1),(X2,Y2))=","((X1+X2)/2,(Y1+Y2)/2)",MidPointOfTwoPointFunc) -factoring = Generator("Subtraction", 21, "x^2+(x1+x2)+x1*x2", "(x-x1)(x-x2)", factoringFunc) \ No newline at end of file +factoring = Generator("Subtraction", 21, "x^2+(x1+x2)+x1*x2", "(x-x1)(x-x2)", factoringFunc) +linearEquations = Generator("Linear Equations", 22, "2x+5y=20 & 3x+6y=12", "x=-20 & y=12", linearEquationsFunc) \ No newline at end of file