Create solver_mixture.py
Browse files- solver_mixture.py +44 -0
solver_mixture.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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_mixture(text: str) -> Optional[SolverResult]:
|
| 10 |
+
|
| 11 |
+
lower = text.lower()
|
| 12 |
+
|
| 13 |
+
if "mixture" not in lower and "solution" not in lower and "%" not in lower:
|
| 14 |
+
return None
|
| 15 |
+
|
| 16 |
+
nums = [float(x) for x in re.findall(r"\d+(?:\.\d+)?", lower)]
|
| 17 |
+
|
| 18 |
+
if len(nums) < 3:
|
| 19 |
+
return None
|
| 20 |
+
|
| 21 |
+
p1 = nums[0] / 100
|
| 22 |
+
p2 = nums[1] / 100
|
| 23 |
+
target = nums[2] / 100
|
| 24 |
+
|
| 25 |
+
if p1 == p2:
|
| 26 |
+
return None
|
| 27 |
+
|
| 28 |
+
ratio = (target - p2) / (p1 - target)
|
| 29 |
+
|
| 30 |
+
if ratio <= 0:
|
| 31 |
+
return None
|
| 32 |
+
|
| 33 |
+
return SolverResult(
|
| 34 |
+
domain="quant",
|
| 35 |
+
solved=True,
|
| 36 |
+
topic="mixture",
|
| 37 |
+
answer_value=f"{ratio:g}",
|
| 38 |
+
internal_answer=f"{ratio:g}",
|
| 39 |
+
steps=[
|
| 40 |
+
"Use the mixture equation.",
|
| 41 |
+
"Set up the weighted average of concentrations.",
|
| 42 |
+
"Solve for the ratio of quantities."
|
| 43 |
+
]
|
| 44 |
+
)
|