j-js commited on
Commit
121fd39
·
verified ·
1 Parent(s): abb4ceb

Create solver_absolute_value.py

Browse files
Files changed (1) hide show
  1. solver_absolute_value.py +69 -0
solver_absolute_value.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import re
4
+ from typing import Optional
5
+
6
+ from models import SolverResult
7
+
8
+
9
+ def solve_absolute_value(text: str) -> Optional[SolverResult]:
10
+ lower = (text or "").lower()
11
+
12
+ if "|" not in text and "absolute value" not in lower and "abs" not in lower:
13
+ return None
14
+
15
+ # |x| = a
16
+ m = re.search(r"\|?\s*x\s*\|?\s*=\s*(-?\d+(?:\.\d+)?)", text.replace(" ", ""))
17
+ if m:
18
+ a = float(m.group(1))
19
+ if a < 0:
20
+ return SolverResult(
21
+ domain="quant",
22
+ solved=True,
23
+ topic="absolute_value",
24
+ answer_value="no solution",
25
+ internal_answer="no solution",
26
+ steps=[
27
+ "Absolute value cannot equal a negative number.",
28
+ ],
29
+ )
30
+ return SolverResult(
31
+ domain="quant",
32
+ solved=True,
33
+ topic="absolute_value",
34
+ answer_value=f"{a:g} and {-a:g}",
35
+ internal_answer=f"{a:g} and {-a:g}",
36
+ steps=[
37
+ "For |x| = a with a ≥ 0, x = a or x = -a.",
38
+ ],
39
+ )
40
+
41
+ # |x - a| = b
42
+ m = re.search(r"\|x-(-?\d+(?:\.\d+)?)\|=(-?\d+(?:\.\d+)?)", text.replace(" ", ""))
43
+ if m:
44
+ a = float(m.group(1))
45
+ b = float(m.group(2))
46
+ if b < 0:
47
+ return SolverResult(
48
+ domain="quant",
49
+ solved=True,
50
+ topic="absolute_value",
51
+ answer_value="no solution",
52
+ internal_answer="no solution",
53
+ steps=["Absolute value cannot equal a negative number."],
54
+ )
55
+ x1 = a + b
56
+ x2 = a - b
57
+ return SolverResult(
58
+ domain="quant",
59
+ solved=True,
60
+ topic="absolute_value",
61
+ answer_value=f"{x1:g} and {x2:g}",
62
+ internal_answer=f"{x1:g} and {x2:g}",
63
+ steps=[
64
+ "Split the absolute value equation into two cases.",
65
+ "Solve x − a = b and x − a = -b.",
66
+ ],
67
+ )
68
+
69
+ return None