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 
 
 
 
 
 
074e4ab
 
 
 
670ac1a
 
 
 
 
074e4ab
 
 
670ac1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
074e4ab
670ac1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
from __future__ import annotations

import math
import re
from statistics import mean, median
from typing import Dict, List, Optional, Tuple

try:
    import sympy as sp
except Exception:
    sp = None

from models import SolverResult
from utils import clean_math_text, normalize_spaces


def extract_choices(text: str) -> Dict[str, str]:
    text = text or ""
    matches = list(
        re.finditer(
            r"(?i)\b([A-E])[\)\.:]\s*(.*?)(?=\s+\b[A-E][\)\.:]\s*|$)",
            text,
        )
    )
    return {m.group(1).upper(): normalize_spaces(m.group(2)) for m in matches}


def has_answer_choices(text: str) -> bool:
    return len(extract_choices(text)) >= 3


def is_quant_question(text: str) -> bool:
    lower = clean_math_text(text).lower()
    keywords = [
        "solve", "equation", "percent", "ratio", "probability", "mean", "median",
        "average", "sum", "difference", "product", "quotient", "triangle", "circle",
        "rectangle", "area", "perimeter", "volume", "algebra", "integer", "divisible",
        "number", "fraction", "decimal", "geometry", "distance", "speed", "work",
        "remainder", "discount",
    ]
    if any(k in lower for k in keywords):
        return True
    if "=" in lower and re.search(r"[a-z]", lower):
        return True
    if re.search(r"\d", lower) and ("?" in lower or has_answer_choices(lower)):
        return True
    return False


def _prepare_expression(expr: str) -> str:
    expr = clean_math_text(expr).strip()
    expr = expr.replace("^", "**")

    # remove common prompt wrappers
    expr = re.sub(r"(?i)^\s*(solve|simplify|evaluate|find|what is|compute)\s*:?\s*", "", expr)

    # implicit multiplication
    expr = re.sub(r"(\d)\s*\(", r"\1*(", expr)
    expr = re.sub(r"\)\s*(\d)", r")*\1", expr)
    expr = re.sub(r"(\d)\s*([a-zA-Z])", r"\1*\2", expr)
    expr = re.sub(r"([a-zA-Z])\s*\(", r"\1*(", expr)
    expr = re.sub(r"\)\s*([a-zA-Z])", r")*\1", expr)
    expr = re.sub(r"([a-zA-Z])\s+([a-zA-Z])", r"\1*\2", expr)

    return expr


def _clean_equation_candidate(text: str) -> str:
    s = clean_math_text(text).strip()

    # remove leading prompt phrases but keep equation content
    s = re.sub(r"(?i)^\s*(solve|simplify|evaluate|find)\s*:?\s*", "", s)
    s = re.sub(r"(?i)^\s*how do i solve\s*:?\s*", "", s)
    s = re.sub(r"(?i)^\s*what is\s+", "", s)
    s = normalize_spaces(s)
    return s


def _extract_equation(text: str) -> Optional[str]:
    cleaned = _clean_equation_candidate(text)
    if "=" not in cleaned:
        return None

    # first try: take the full equation-looking span
    patterns = [
        r"([A-Za-z0-9\.\+\-\*/\^\(\)\s]+=[A-Za-z0-9\.\+\-\*/\^\(\)\s]+)",
    ]

    for pattern in patterns:
        for m in re.finditer(pattern, cleaned):
            candidate = normalize_spaces(m.group(1))
            if "=" not in candidate:
                continue
            if re.search(r"[a-z]", candidate.lower()):
                return candidate

    # fallback: split once on equals and trim to likely expression zones
    parts = cleaned.split("=", 1)
    if len(parts) != 2:
        return None

    lhs = parts[0].strip(" .,:;!?")
    rhs = parts[1].strip(" .,:;!?")

    if not lhs or not rhs:
        return None

    candidate = f"{lhs} = {rhs}"
    if re.search(r"[a-z]", candidate.lower()):
        return candidate

    return None


def _extract_variable_names(expr: str) -> List[str]:
    # catches x in "3x + 5 = 20" as well as standalone x
    vars_found = sorted(set(re.findall(r"[a-z]", expr.lower())))
    # avoid treating common words as many variables by keeping only likely algebra variables first
    preferred = [v for v in vars_found if v in {"x", "y", "z", "n"}]
    return preferred or vars_found


def _parse_number(text: str) -> Optional[float]:
    raw = clean_math_text(text).strip().lower()

    pct = re.fullmatch(r"(-?\d+(?:\.\d+)?)%", raw.replace(" ", ""))
    if pct:
        return float(pct.group(1)) / 100.0

    frac = re.fullmatch(r"(-?\d+)\s*/\s*(-?\d+)", raw)
    if frac:
        den = float(frac.group(2))
        if den == 0:
            return None
        return float(frac.group(1)) / den

    try:
        return float(
            eval(
                _prepare_expression(raw),
                {"__builtins__": {}},
                {"sqrt": math.sqrt, "pi": math.pi},
            )
        )
    except Exception:
        return None


def _best_choice(answer_value: float, choices: Dict[str, str]) -> Optional[str]:
    best_letter = None
    best_diff = float("inf")

    for letter, raw in choices.items():
        parsed = _parse_number(raw)
        if parsed is None:
            continue
        diff = abs(parsed - answer_value)
        if diff < best_diff:
            best_diff = diff
            best_letter = letter

    if best_letter is not None and best_diff <= 1e-6:
        return best_letter
    return None


def _make_result(
    *,
    topic: str,
    answer_value: str,
    internal_answer: Optional[str] = None,
    steps: Optional[List[str]] = None,
    choices_text: str = "",
) -> SolverResult:
    answer_float = _parse_number(answer_value)
    choices = extract_choices(choices_text)
    answer_letter = _best_choice(answer_float, choices) if (answer_float is not None and choices) else None

    return SolverResult(
        domain="quant",
        solved=True,
        topic=topic,
        answer_value=answer_value,
        answer_letter=answer_letter,
        internal_answer=internal_answer or answer_value,
        steps=steps or [],
    )


def _solve_successive_percent(text: str) -> Optional[SolverResult]:
    lower = clean_math_text(text).lower()

    pattern = re.findall(
        r"(increase|decrease|discount|mark(?:ed)?\s*up|mark(?:ed)?\s*down|rise|fall)\s+by\s+(\d+(?:\.\d+)?)\s*(?:%|percent)",
        lower,
    )
    if len(pattern) < 2:
        pattern = re.findall(
            r"(\d+(?:\.\d+)?)\s*(?:%|percent)\s+(increase|decrease|discount|rise|fall)",
            lower,
        )
        pattern = [(op, pct) for pct, op in pattern]

    if len(pattern) < 2:
        return None

    multiplier = 1.0
    step_lines: List[str] = []

    for op, pct_raw in pattern:
        pct = float(pct_raw)
        if any(k in op for k in ["decrease", "discount", "down", "fall"]):
            factor = 1 - pct / 100.0
            step_lines.append(f"A {pct:g}% decrease means multiply by {factor:g}.")
        else:
            factor = 1 + pct / 100.0
            step_lines.append(f"A {pct:g}% increase means multiply by {factor:g}.")
        multiplier *= factor

    net_change = (multiplier - 1.0) * 100.0
    direction = "increase" if net_change >= 0 else "decrease"
    magnitude = abs(net_change)

    return _make_result(
        topic="percent",
        answer_value=f"{magnitude:g}%",
        internal_answer=f"net {direction} of {magnitude:g}%",
        steps=step_lines + [f"Combine the multipliers to find the overall percent change."],
        choices_text=text,
    )


def _extract_ratio_labels(text: str) -> Optional[Tuple[str, str]]:
    m = re.search(r"ratio of ([a-z ]+?) to ([a-z ]+?) is \d+\s*:\s*\d+", text.lower())
    if not m:
        return None
    left = normalize_spaces(m.group(1)).rstrip("s")
    right = normalize_spaces(m.group(2)).rstrip("s")
    return left, right


def _solve_ratio_total(text: str) -> Optional[SolverResult]:
    lower = clean_math_text(text).lower()

    ratio_match = re.search(r"(\d+)\s*:\s*(\d+)", lower)
    total_match = re.search(r"(?:total|altogether|in all|sum)\s*(?:is|=|of)?\s*(\d+)", lower)

    if not ratio_match or not total_match:
        return None

    a = int(ratio_match.group(1))
    b = int(ratio_match.group(2))
    total = int(total_match.group(1))

    part_sum = a + b
    if part_sum == 0:
        return None

    unit = total / part_sum
    left_value = a * unit
    right_value = b * unit

    labels = _extract_ratio_labels(lower)
    requested_value = left_value

    if labels:
        left_label, right_label = labels
        if left_label in lower and re.search(rf"how many {re.escape(left_label)}", lower):
            requested_value = left_value
        elif right_label in lower and re.search(rf"how many {re.escape(right_label)}", lower):
            requested_value = right_value
        else:
            requested_value = left_value

    return _make_result(
        topic="ratio",
        answer_value=f"{requested_value:g}",
        internal_answer=f"{requested_value:g}",
        steps=[
            f"Add the ratio parts: {a} + {b} = {part_sum}.",
            f"Find the value of one ratio part using the total.",
            f"Multiply by the required ratio part.",
        ],
        choices_text=text,
    )


def _solve_remainder(text: str) -> Optional[SolverResult]:
    lower = clean_math_text(text).lower()

    m = re.search(r"remainder .*? when (\d+) is divided by (\d+)", lower)
    if not m:
        m = re.search(r"(\d+)\s*(?:mod|%)\s*(\d+)", lower)
    if not m:
        return None

    a = int(m.group(1))
    b = int(m.group(2))
    if b == 0:
        return None

    r = a % b

    return _make_result(
        topic="number_theory",
        answer_value=str(r),
        internal_answer=str(r),
        steps=[
            f"Divide {a} by {b}.",
            "Keep the amount left over after division.",
        ],
        choices_text=text,
    )


def _solve_percent(text: str) -> Optional[SolverResult]:
    lower = clean_math_text(text).lower()
    choices = extract_choices(text)

    patterns = [
        r"(\d+(?:\.\d+)?)\s*(?:%|percent)\s+of\s+(?:a\s+)?number\s+is\s+(\d+(?:\.\d+)?)",
        r"(\d+(?:\.\d+)?)\s*(?:%|percent)\s+of\s+([a-z])\s+is\s+(\d+(?:\.\d+)?)",
        r"(\d+(?:\.\d+)?)\s*(?:%|percent)\s+of\s+([a-z])\s*=\s*(\d+(?:\.\d+)?)",
    ]

    for pat in patterns:
        m = re.search(pat, lower)
        if not m:
            continue

        if len(m.groups()) == 2:
            p = float(m.group(1))
            value = float(m.group(2))
        else:
            p = float(m.group(1))
            value = float(m.group(3))

        if p == 0:
            return None

        ans = value / (p / 100.0)
        answer_letter = _best_choice(ans, choices) if choices else None

        return SolverResult(
            domain="quant",
            solved=True,
            topic="percent",
            answer_value=f"{ans:g}",
            answer_letter=answer_letter,
            internal_answer=f"{ans:g}",
            steps=[
                "Let the unknown quantity be a variable.",
                f"Convert {p:g}% into its decimal form.",
                "Write an equation for the part and whole relationship.",
                "Solve that equation for the unknown.",
            ],
        )

    m = re.search(r"what is\s+(\d+(?:\.\d+)?)\s*(?:%|percent)\s+of\s+(\d+(?:\.\d+)?)", lower)
    if m:
        p = float(m.group(1))
        n = float(m.group(2))
        ans = p / 100.0 * n
        answer_letter = _best_choice(ans, choices) if choices else None

        return SolverResult(
            domain="quant",
            solved=True,
            topic="percent",
            answer_value=f"{ans:g}",
            answer_letter=answer_letter,
            internal_answer=f"{ans:g}",
            steps=[
                f"Convert {p:g}% to a decimal.",
                f"Multiply that decimal by {n}.",
            ],
        )

    return None


def _solve_mean_median(text: str) -> Optional[SolverResult]:
    lower = clean_math_text(text).lower()
    nums = [float(n) for n in re.findall(r"-?\d+(?:\.\d+)?", lower)]
    if not nums:
        return None

    if "mean" in lower or "average" in lower:
        ans = mean(nums)
        return SolverResult(
            domain="quant",
            solved=True,
            topic="statistics",
            answer_value=f"{ans:g}",
            internal_answer=f"{ans:g}",
            steps=["Add the values.", f"Divide by {len(nums)}."],
        )

    if "median" in lower:
        ans = median(nums)
        return SolverResult(
            domain="quant",
            solved=True,
            topic="statistics",
            answer_value=f"{ans:g}",
            internal_answer=f"{ans:g}",
            steps=["Order the values.", "Take the middle value."],
        )

    return None


def _solve_linear_equation(text: str) -> Optional[SolverResult]:
    if sp is None:
        return None

    expr = _extract_equation(text)
    if not expr:
        return None

    try:
        lhs, rhs = expr.split("=", 1)
        lhs_prepped = _prepare_expression(lhs)
        rhs_prepped = _prepare_expression(rhs)

        symbols = _extract_variable_names(expr)
        if not symbols:
            return None

        var_name = symbols[0]
        var = sp.symbols(var_name)

        equation = sp.Eq(sp.sympify(lhs_prepped), sp.sympify(rhs_prepped))
        sol = sp.solve(equation, var)
        if not sol:
            return None

        value = sol[0]
        try:
            as_float = float(value)
        except Exception:
            as_float = None

        choices = extract_choices(text)

        return SolverResult(
            domain="quant",
            solved=True,
            topic="algebra",
            answer_value=str(value),
            answer_letter=_best_choice(as_float, choices) if (as_float is not None and choices) else None,
            internal_answer=f"{var_name} = {value}",
            steps=[
                "Treat the statement as an equation.",
                "Undo operations on both sides to isolate the variable.",
                "Keep simplifying until the variable is alone.",
            ],
        )
    except Exception:
        return None


def solve_quant(text: str) -> SolverResult:
    text = text or ""

    for fn in (
        _solve_successive_percent,
        _solve_ratio_total,
        _solve_remainder,
        _solve_percent,
        _solve_mean_median,
        _solve_linear_equation,
    ):
        result = fn(text)
        if result is not None:
            return result

    return SolverResult(
        domain="quant",
        solved=False,
        topic="general_quant",
        reply="This looks quantitative, but it does not match a strong rule-based pattern yet.",
        steps=[
            "Identify the quantity the question wants.",
            "Translate the wording into an equation, ratio, or diagram.",
            "Carry out the calculation carefully.",
        ],
    )