File size: 22,411 Bytes
fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd fc53c42 c13d1bd | 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 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 | from __future__ import annotations
import math
import re
from typing import Optional, List
from models import SolverResult
def _nums(text: str) -> List[float]:
return [float(x) for x in re.findall(r"-?\d+(?:\.\d+)?", text)]
def _clean(text: str) -> str:
return re.sub(r"\s+", " ", (text or "").strip()).lower()
def _pct_to_decimal(p: float) -> float:
return p / 100.0
def _safe_fmt(x: float) -> str:
return f"{x:g}"
def _build_result(
*,
topic: str,
internal_answer: Optional[float] = None,
ask: str,
given: str,
method: List[str],
) -> SolverResult:
return SolverResult(
domain="quant",
solved=True,
topic=topic,
answer_value=None, # never expose final answer
internal_answer=_safe_fmt(internal_answer) if internal_answer is not None else None,
steps=[
f"What the question is asking: {ask}",
f"What is given: {given}",
*method,
],
)
def _fallback_percent() -> SolverResult:
return SolverResult(
domain="quant",
solved=False,
topic="percent",
answer_value=None,
internal_answer=None,
steps=[
"What the question is asking: identify whether you need the part, the whole, the percent, or the percent change.",
"Key relationships:",
"• part = percent × whole",
"• percent = part ÷ whole × 100",
"• percent change = change ÷ original × 100",
"Start by labeling the numbers in the problem as original, new, part, whole, rate, or time.",
],
)
def solve_percent(text: str) -> Optional[SolverResult]:
raw = text or ""
lower = _clean(raw)
percent_words = [
"%", "percent", "percentage", "pct",
"increase", "decrease", "discount", "markup",
"profit", "loss", "interest", "percentile",
"tax", "tip", "sale"
]
if not any(w in lower for w in percent_words):
return None
nums = _nums(lower)
# --------------------------------------------------
# 1) BASIC PART: "What is 25% of 80?"
# --------------------------------------------------
m = re.search(
r"what\s+is\s+(\d+(?:\.\d+)?)\s*(?:%|percent|pct)?\s+of\s+(-?\d+(?:\.\d+)?)",
lower
)
if m:
pct = float(m.group(1))
whole = float(m.group(2))
result = _pct_to_decimal(pct) * whole
return _build_result(
topic="percent",
internal_answer=result,
ask="Find the part when the percent and whole are known.",
given=f"percent = {pct}%, whole = {whole}",
method=[
"Convert the percent to a decimal.",
"Use part = percent × whole.",
"Multiply the decimal percent by the whole.",
],
)
# --------------------------------------------------
# 2) BASIC WHOLE: "40% of a number is 24"
# Also handles "60 is 25% of what number?"
# --------------------------------------------------
m = re.search(
r"(\d+(?:\.\d+)?)\s*(?:%|percent|pct)\s+of\s+(?:a\s+number|number|what\s+number|x|y|an?\s+amount|the\s+number).*?\bis\s+(-?\d+(?:\.\d+)?)",
lower
)
if m:
pct = float(m.group(1))
part = float(m.group(2))
dec = _pct_to_decimal(pct)
if dec == 0:
return _fallback_percent()
result = part / dec
return _build_result(
topic="percent",
internal_answer=result,
ask="Find the whole when the percent and part are known.",
given=f"percent = {pct}%, part = {part}",
method=[
"Convert the percent to a decimal.",
"Use part = percent × whole.",
"Rearrange to whole = part ÷ percent.",
],
)
m = re.search(
r"(-?\d+(?:\.\d+)?)\s+is\s+(\d+(?:\.\d+)?)\s*(?:%|percent|pct)\s+of\s+(?:what|which)\s+(?:number|value|amount)",
lower
)
if m:
part = float(m.group(1))
pct = float(m.group(2))
dec = _pct_to_decimal(pct)
if dec == 0:
return _fallback_percent()
result = part / dec
return _build_result(
topic="percent",
internal_answer=result,
ask="Find the original whole from a known part and percent.",
given=f"part = {part}, percent = {pct}%",
method=[
"Convert the percent to decimal form.",
"Use part = percent × whole.",
"Solve for the whole by dividing the part by the decimal percent.",
],
)
# --------------------------------------------------
# 3) WHAT PERCENT: "24 is what percent of 60?"
# --------------------------------------------------
m = re.search(
r"(-?\d+(?:\.\d+)?)\s+is\s+what\s+(?:%|percent|percentage)\s+of\s+(-?\d+(?:\.\d+)?)",
lower
)
if m:
part = float(m.group(1))
whole = float(m.group(2))
if whole == 0:
return _fallback_percent()
result = (part / whole) * 100.0
return _build_result(
topic="percent",
internal_answer=result,
ask="Find what percent one value is of another.",
given=f"part = {part}, whole = {whole}",
method=[
"Use percent = part ÷ whole × 100.",
"Divide the part by the whole.",
"Convert the result to percent form.",
],
)
m = re.search(
r"what\s+(?:%|percent|percentage)\s+of\s+(-?\d+(?:\.\d+)?)\s+is\s+(-?\d+(?:\.\d+)?)",
lower
)
if m:
whole = float(m.group(1))
part = float(m.group(2))
if whole == 0:
return _fallback_percent()
result = (part / whole) * 100.0
return _build_result(
topic="percent",
internal_answer=result,
ask="Find the percent represented by the part out of the whole.",
given=f"whole = {whole}, part = {part}",
method=[
"Use percent = part ÷ whole × 100.",
"Divide the part by the whole.",
"Then convert to a percent.",
],
)
# --------------------------------------------------
# 4) PERCENT CHANGE: "from 80 to 60"
# --------------------------------------------------
m = re.search(
r"(?:what\s+(?:is\s+the\s+)?)?(?:percent\s+)?(increase|decrease|change).*?from\s+(-?\d+(?:\.\d+)?)\s+to\s+(-?\d+(?:\.\d+)?)",
lower
)
if m:
direction = m.group(1)
old = float(m.group(2))
new = float(m.group(3))
if old == 0:
return _fallback_percent()
result = ((new - old) / old) * 100.0
return _build_result(
topic="percent_change",
internal_answer=abs(result) if direction in ("increase", "decrease") else result,
ask="Find the percent change relative to the original value.",
given=f"original = {old}, new = {new}",
method=[
"Find the change: new − original.",
"Use the original value in the denominator.",
"Compute change ÷ original × 100.",
"Match the sign or wording to increase vs decrease.",
],
)
m = re.search(
r"from\s+(-?\d+(?:\.\d+)?)\s+to\s+(-?\d+(?:\.\d+)?)",
lower
)
if m and ("percent" in lower or "%" in lower):
old = float(m.group(1))
new = float(m.group(2))
if old == 0:
return _fallback_percent()
result = ((new - old) / old) * 100.0
return _build_result(
topic="percent_change",
internal_answer=result,
ask="Find the percent change between the starting value and ending value.",
given=f"original = {old}, new = {new}",
method=[
"Subtract to find the change.",
"Divide by the original value, not the new value.",
"Multiply by 100 to convert to percent form.",
],
)
# --------------------------------------------------
# 5) NEW VALUE AFTER CHANGE: "80 increased by 25%"
# --------------------------------------------------
m = re.search(
r"(-?\d+(?:\.\d+)?)\s+(?:is\s+)?(increased|decreased|reduced)\s+by\s+(\d+(?:\.\d+)?)\s*%",
lower
)
if m:
original = float(m.group(1))
direction = m.group(2)
pct = float(m.group(3))
dec = _pct_to_decimal(pct)
multiplier = 1 + dec if direction == "increased" else 1 - dec
result = original * multiplier
return _build_result(
topic="percent_change",
internal_answer=result,
ask="Find the new value after a percent increase or decrease.",
given=f"original = {original}, change = {pct}% {direction}",
method=[
"Convert the percent to a decimal.",
"Use a multiplier.",
"For increase, multiply by 1 + p.",
"For decrease, multiply by 1 − p.",
],
)
# --------------------------------------------------
# 6) ORIGINAL BEFORE CHANGE:
# "After a 20% increase, a number is 120"
# "After a 25% discount, the price is 90"
# --------------------------------------------------
m = re.search(
r"after\s+(?:an?\s+)?(\d+(?:\.\d+)?)\s*%\s*(increase|decrease|discount|markup|reduction|loss|gain).*?(?:is|becomes|equals|was|now\s+is)\s+(-?\d+(?:\.\d+)?)",
lower
)
if m:
pct = float(m.group(1))
kind = m.group(2)
final_value = float(m.group(3))
dec = _pct_to_decimal(pct)
if kind in ("increase", "markup", "gain"):
multiplier = 1 + dec
else:
multiplier = 1 - dec
if multiplier == 0:
return _fallback_percent()
result = final_value / multiplier
return _build_result(
topic="reverse_percent",
internal_answer=result,
ask="Find the original value before a percent change.",
given=f"final value = {final_value}, change = {pct}% {kind}",
method=[
"Translate the percent change into a multiplier.",
"Increase/markup/gain → original × (1 + p).",
"Decrease/discount/loss → original × (1 − p).",
"Solve by dividing the final value by that multiplier.",
],
)
# --------------------------------------------------
# 7) PERCENT GREATER / LESS THAN
# "What percent greater is 80 than 50?"
# "80 is what percent greater than 50?"
# --------------------------------------------------
m = re.search(
r"what\s+percent\s+(greater|less)\s+(?:is\s+)?(-?\d+(?:\.\d+)?)\s+than\s+(-?\d+(?:\.\d+)?)",
lower
)
if m:
relation = m.group(1)
a = float(m.group(2))
b = float(m.group(3))
if b == 0:
return _fallback_percent()
result = ((a - b) / b) * 100.0
return _build_result(
topic="percent_comparison",
internal_answer=abs(result),
ask=f"Compare one value to another and express the difference as a percent of the reference value.",
given=f"compare {a} to reference {b}",
method=[
"Find the difference between the two values.",
"Use the second value as the reference base.",
"Compute difference ÷ reference × 100.",
f"Interpret the result as percent {relation}.",
],
)
m = re.search(
r"(-?\d+(?:\.\d+)?)\s+is\s+what\s+percent\s+(greater|less)\s+than\s+(-?\d+(?:\.\d+)?)",
lower
)
if m:
a = float(m.group(1))
relation = m.group(2)
b = float(m.group(3))
if b == 0:
return _fallback_percent()
result = ((a - b) / b) * 100.0
return _build_result(
topic="percent_comparison",
internal_answer=abs(result),
ask="Find by what percent one value is greater or less than another.",
given=f"value = {a}, reference = {b}",
method=[
"Subtract to find the difference.",
"Divide by the reference value named after 'than'.",
"Multiply by 100 to get the percent comparison.",
f"Use the wording to label it as {relation}.",
],
)
# --------------------------------------------------
# 8) DISCOUNT / MARKUP / TAX / TIP
# --------------------------------------------------
m = re.search(
r"(?:price|cost|bill|amount)\s+of\s+(-?\d+(?:\.\d+)?)\s+.*?(discount|markup|tax|tip)\s+of\s+(\d+(?:\.\d+)?)\s*%",
lower
)
if m:
base = float(m.group(1))
kind = m.group(2)
pct = float(m.group(3))
dec = _pct_to_decimal(pct)
if kind == "discount":
result = base * (1 - dec)
else:
result = base * (1 + dec)
return _build_result(
topic="percent_application",
internal_answer=result,
ask=f"Apply a {kind} percentage to a base amount.",
given=f"base = {base}, rate = {pct}% {kind}",
method=[
"Convert the percent to decimal form.",
"For discount, subtract the percent part from the base or multiply by 1 − p.",
"For markup, tax, or tip, add the percent part or multiply by 1 + p.",
],
)
# --------------------------------------------------
# 9) PROFIT / LOSS
# --------------------------------------------------
m = re.search(
r"(?:cost price|cp|cost)\s+(-?\d+(?:\.\d+)?)\s+.*?(?:selling price|sp|sold for)\s+(-?\d+(?:\.\d+)?)",
lower
)
if m and ("profit" in lower or "loss" in lower or "percent" in lower or "%" in lower):
cost = float(m.group(1))
sell = float(m.group(2))
if cost == 0:
return _fallback_percent()
result = ((sell - cost) / cost) * 100.0
return _build_result(
topic="profit_loss",
internal_answer=result,
ask="Find the profit or loss percent relative to the cost price.",
given=f"cost price = {cost}, selling price = {sell}",
method=[
"Find profit/loss = selling price − cost price.",
"Use cost price as the denominator.",
"Compute (profit or loss) ÷ cost price × 100.",
],
)
# --------------------------------------------------
# 10) SUCCESSIVE PERCENT CHANGES
# "increased by 20% and then decreased by 10%"
# --------------------------------------------------
m = re.search(
r"(-?\d+(?:\.\d+)?)\s+.*?(increase(?:d)?|decrease(?:d)?|reduced)\s+by\s+(\d+(?:\.\d+)?)\s*%.*?(increase(?:d)?|decrease(?:d)?|reduced)\s+by\s+(\d+(?:\.\d+)?)\s*%",
lower
)
if m:
original = float(m.group(1))
op1 = m.group(2)
p1 = _pct_to_decimal(float(m.group(3)))
op2 = m.group(4)
p2 = _pct_to_decimal(float(m.group(5)))
mult1 = 1 + p1 if "increase" in op1 else 1 - p1
mult2 = 1 + p2 if "increase" in op2 else 1 - p2
result = original * mult1 * mult2
return _build_result(
topic="successive_percent_change",
internal_answer=result,
ask="Apply two percent changes in sequence.",
given=f"start = {original}, first change = {m.group(3)}%, second change = {m.group(5)}%",
method=[
"Convert each percent to a decimal.",
"Turn each change into a multiplier.",
"Apply the first multiplier, then apply the second to the new value.",
"Do not add or subtract the percentages directly unless the problem specifically asks for net rate in a special setup.",
],
)
# --------------------------------------------------
# 11) PERCENT DIFFERENCE
# --------------------------------------------------
if "percent difference" in lower:
m = re.search(
r"between\s+(-?\d+(?:\.\d+)?)\s+and\s+(-?\d+(?:\.\d+)?)",
lower
)
if m:
a = float(m.group(1))
b = float(m.group(2))
avg = (a + b) / 2.0
if avg == 0:
return _fallback_percent()
result = abs(a - b) / avg * 100.0
return _build_result(
topic="percent_difference",
internal_answer=result,
ask="Find the percent difference between two values.",
given=f"values = {a} and {b}",
method=[
"Find the absolute difference between the two values.",
"Find the average of the two values.",
"Use percent difference = difference ÷ average × 100.",
"Keep this separate from percent change, which uses the original value as the denominator.",
],
)
# --------------------------------------------------
# 12) SIMPLE INTEREST
# --------------------------------------------------
if "simple interest" in lower:
m = re.search(
r"(-?\d+(?:\.\d+)?)\s+.*?(\d+(?:\.\d+)?)\s*%\s+.*?(\d+(?:\.\d+)?)\s*(year|years|month|months)",
lower
)
if m:
principal = float(m.group(1))
rate = float(m.group(2))
time = float(m.group(3))
unit = m.group(4)
t_years = time / 12.0 if "month" in unit else time
result = principal * _pct_to_decimal(rate) * t_years
return _build_result(
topic="simple_interest",
internal_answer=result,
ask="Find interest earned using the simple interest formula.",
given=f"principal = {principal}, rate = {rate}%, time = {time} {unit}",
method=[
"Use simple interest = principal × rate × time.",
"Convert the rate to decimal form.",
"Make sure time is in the same units as the rate.",
"If the rate is annual and time is in months, convert months to years first.",
],
)
# --------------------------------------------------
# 13) COMPOUND INTEREST
# --------------------------------------------------
if "compound interest" in lower or "compounded" in lower:
m = re.search(
r"(-?\d+(?:\.\d+)?)\s+.*?(\d+(?:\.\d+)?)\s*%\s+.*?compounded\s+(annually|semiannually|quarterly|monthly).*?(\d+(?:\.\d+)?)\s+year",
lower
)
if m:
principal = float(m.group(1))
rate = float(m.group(2))
comp = m.group(3)
years = float(m.group(4))
c_map = {
"annually": 1,
"semiannually": 2,
"quarterly": 4,
"monthly": 12,
}
c = c_map[comp]
result = principal * ((1 + _pct_to_decimal(rate) / c) ** (c * years))
return _build_result(
topic="compound_interest",
internal_answer=result,
ask="Find the final balance after compound interest.",
given=f"principal = {principal}, rate = {rate}%, compounded {comp}, time = {years} years",
method=[
"Use the compound interest form A = P(1 + r/c)^(ct).",
"Convert the rate to decimal form.",
"Match the compounding frequency to the correct value of c.",
"Substitute carefully before evaluating.",
],
)
# --------------------------------------------------
# 14) PERCENTILE
# --------------------------------------------------
if "percentile" in lower:
# Simple interpretation pattern:
# "80th percentile out of 120"
m = re.search(
r"(\d+(?:\.\d+)?)(?:st|nd|rd|th)\s+percentile\s+out\s+of\s+(\d+(?:\.\d+)?)",
lower
)
if m:
pct = float(m.group(1))
total = float(m.group(2))
result = _pct_to_decimal(pct) * total
return _build_result(
topic="percentile",
internal_answer=result,
ask="Interpret a percentile as the proportion of the group below that score.",
given=f"{pct}th percentile out of {total}",
method=[
"Convert the percentile to a decimal proportion.",
"Multiply by the total group size to estimate how many are below that position.",
"Check the wording carefully: percentile usually refers to the percentage of values below a score.",
],
)
# --------------------------------------------------
# 15) GENERIC HIDDEN PERCENT LANGUAGE
# "out of", "of", "remaining", "sale", etc.
# --------------------------------------------------
if len(nums) >= 2 and any(k in lower for k in ["out of", "remaining", "sale", "discount", "tax", "tip", "profit", "loss"]):
return SolverResult(
domain="quant",
solved=False,
topic="percent",
answer_value=None,
internal_answer=None,
steps=[
"This looks like a percent-style word problem.",
"What the question is asking: first decide whether the unknown is the part, whole, rate, original value, or changed value.",
"Useful checks:",
"• 'out of' often signals part/whole",
"• 'discount', 'tax', 'tip', 'markup' often signal multiplier use",
"• 'profit'/'loss' uses cost price as the base",
"• 'from ... to ...' often signals percent change",
],
)
return _fallback_percent() |