j-js commited on
Commit
d72f959
·
verified ·
1 Parent(s): 6553aeb

Update solver_absolute_value.py

Browse files
Files changed (1) hide show
  1. solver_absolute_value.py +14 -9
solver_absolute_value.py CHANGED
@@ -7,13 +7,15 @@ 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:
@@ -34,12 +36,13 @@ def solve_absolute_value(text: str) -> Optional[SolverResult]:
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))
@@ -50,7 +53,9 @@ def solve_absolute_value(text: str) -> Optional[SolverResult]:
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
@@ -61,8 +66,8 @@ def solve_absolute_value(text: str) -> Optional[SolverResult]:
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
 
 
7
 
8
 
9
  def solve_absolute_value(text: str) -> Optional[SolverResult]:
10
+ raw = text or ""
11
+ lower = raw.lower()
12
+ compact = lower.replace(" ", "")
13
 
14
+ if "|" not in raw and "absolute value" not in lower and "abs" not in lower:
15
  return None
16
 
17
  # |x| = a
18
+ m = re.search(r"\|x\|=(-?\d+(?:\.\d+)?)", compact)
19
  if m:
20
  a = float(m.group(1))
21
  if a < 0:
 
36
  answer_value=f"{a:g} and {-a:g}",
37
  internal_answer=f"{a:g} and {-a:g}",
38
  steps=[
39
+ "For |x| = a with a ≥ 0, split into two cases.",
40
+ "The variable can equal the positive value or the negative value.",
41
  ],
42
  )
43
 
44
+ # |x-a| = b
45
+ m = re.search(r"\|x-(-?\d+(?:\.\d+)?)\|=(-?\d+(?:\.\d+)?)", compact)
46
  if m:
47
  a = float(m.group(1))
48
  b = float(m.group(2))
 
53
  topic="absolute_value",
54
  answer_value="no solution",
55
  internal_answer="no solution",
56
+ steps=[
57
+ "Absolute value cannot equal a negative number.",
58
+ ],
59
  )
60
  x1 = a + b
61
  x2 = a - b
 
66
  answer_value=f"{x1:g} and {x2:g}",
67
  internal_answer=f"{x1:g} and {x2:g}",
68
  steps=[
69
+ "Split the absolute value equation into two linear cases.",
70
+ "Solve the positive case and the negative case.",
71
  ],
72
  )
73