Skip to content

Commit cda62b9

Browse files
Fix tests issue
Connection timeout
1 parent 876ac68 commit cda62b9

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

MathCalc/meth.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
class IndefiniteIntegralSolver:
2+
def __init__(self, expression):
3+
self.expression = expression
4+
5+
def integrate(self):
6+
try:
7+
terms = self.parse_expression(self.expression)
8+
integrated_terms = [self.integrate_term(term) for term in terms]
9+
result = " + ".join(integrated_terms) + " + C"
10+
return result
11+
except Exception as e:
12+
return str(e)
13+
14+
def parse_expression(self, expression):
15+
expression = expression.replace(" ", "")
16+
terms = []
17+
term = ""
18+
in_parentheses = 0
19+
20+
for char in expression:
21+
if char == "(":
22+
in_parentheses += 1
23+
elif char == ")":
24+
in_parentheses -= 1
25+
26+
if char == "+" and in_parentheses == 0:
27+
terms.append(term)
28+
term = ""
29+
else:
30+
term += char
31+
32+
terms.append(term)
33+
return terms
34+
35+
def integrate_term(self, term):
36+
if "x" not in term:
37+
if "/" in term:
38+
num, denom = term.split("/")
39+
return f"{float(num) / float(denom)}*x"
40+
return f"{term}*x"
41+
42+
if "^" in term:
43+
base, exponent = term.split("^")
44+
if base == "x":
45+
new_exponent = float(exponent) + 1
46+
coefficient = 1 / new_exponent
47+
return f"{coefficient}*x^{new_exponent}"
48+
else:
49+
coefficient, base = base.split("*")
50+
new_exponent = float(exponent) + 1
51+
coefficient = float(coefficient) / new_exponent
52+
return f"{coefficient}*x^{new_exponent}"
53+
elif "*" in term:
54+
coefficient, base = term.split("*")
55+
if base == "x":
56+
return f"{float(coefficient) / 2}*x^2"
57+
else:
58+
raise ValueError("Invalid term")
59+
elif term == "x":
60+
return "0.5*x^2"
61+
elif term == "0":
62+
return "0*x"
63+
else:
64+
raise ValueError("Invalid term")

MathCalc/test_integrate.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Test cases for IndefiniteIntegralSolver
2+
from meth import IndefiniteIntegralSolver
3+
4+
5+
def test_indefinite_integral_solver():
6+
test_cases = [
7+
# Easy cases
8+
("3*x^2 + 2*x + 1", "1.0*x^3 + 1.0*x^2 + 1*x + C"),
9+
("x^2 + x + 1", "0.3333333333333333*x^3 + 0.5*x^2 + 1*x + C"),
10+
("5*x + 3", "2.5*x^2 + 3*x + C"),
11+
12+
# Harder cases
13+
("4*x^3 + 3*x^2 + 2*x + 1", "1.0*x^4 + 1.0*x^3 + 1.0*x^2 + 1*x + C"),
14+
("6*x^5 + 5*x^4 + 4*x^3 + 3*x^2 + 2*x + 1", "1.0*x^6 + 1.0*x^5 + 1.0*x^4 + 1.0*x^3 + 1.0*x^2 + 1*x + C"),
15+
16+
# Cases with brackets
17+
("(3*x^2) + (2*x) + 1", "1.0*x^3 + 1.0*x^2 + 1*x + C"),
18+
("(4*x^3) + (3*x^2) + (2*x) + 1", "1.0*x^4 + 1.0*x^3 + 1.0*x^2 + 1*x + C"),
19+
20+
# Edge cases
21+
("x", "0.5*x^2 + C"),
22+
("1", "1*x + C"),
23+
("x^0.5", "0.6666666666666666*x^1.5 + C"),
24+
("1/2", "0.5*x + C"),
25+
("1/x", "-1/2*x^-2 + C"),
26+
("0", "0*x + C"),
27+
("x^2", "0.3333333333333333*x^3 + C"),
28+
("x^3", "0.25*x^4 + C"),
29+
("x^4", "0.2*x^5 + C"),
30+
("x^5", "0.16666666666666666*x^6 + C"),
31+
("(1/2)*(x^2)", "0.16666666666666666*x^3 + C"),
32+
("(-1/2)*(x^-1)", "x^-2"),
33+
("(1/2)*(x^-1/2)", "x^0.5 + C"),
34+
35+
# Invalid cases (should handle gracefully)
36+
("3*x^2 + 2*y + 1", "1.0*x^3 + 2.0*x + 1*x + C"), # y is not a valid variable for integration
37+
("3*x^2 + 2* + 1", "1.0*x^3 + 2.0*x + 1*x + C"), # Invalid term "2*"
38+
]
39+
40+
counter = 0
41+
fail_counter = 0
42+
for expression, expected in test_cases:
43+
solver = IndefiniteIntegralSolver(expression)
44+
result = solver.integrate()
45+
try:
46+
assert result == expected, f"Test failed for expression: {expression}. Expected: {expected}, Got: {result}"
47+
print(f"✅ Test passed for expression: {expression}. Result: {result}")
48+
except AssertionError:
49+
print(f"❌ Test failed for expression: {expression}. Expected: {expected}, Got: {result}")
50+
fail_counter += 1
51+
counter += 1
52+
print(f"\nTotal tests 🧪: {counter}, "
53+
f"\nPassed ✅: {counter - fail_counter}, "
54+
f"\nFailed ❌: {fail_counter}")
55+
56+
# Run the tests
57+
test_indefinite_integral_solver()

0 commit comments

Comments
 (0)