Create solver_combinatorics.py
Browse files- solver_combinatorics.py +95 -0
solver_combinatorics.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import re
|
| 4 |
+
from math import comb, perm, factorial
|
| 5 |
+
from typing import Optional
|
| 6 |
+
|
| 7 |
+
from models import SolverResult
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def solve_combinatorics(text: str) -> Optional[SolverResult]:
|
| 11 |
+
lower = (text or "").lower()
|
| 12 |
+
|
| 13 |
+
if not any(w in lower for w in [
|
| 14 |
+
"combination", "permutation", "arrange", "arrangement",
|
| 15 |
+
"choose", "select", "committee"
|
| 16 |
+
]):
|
| 17 |
+
return None
|
| 18 |
+
|
| 19 |
+
# "how many ways to choose r from n"
|
| 20 |
+
m = re.search(r"choose\s+(\d+)\s+from\s+(\d+)", lower)
|
| 21 |
+
if m:
|
| 22 |
+
r = int(m.group(1))
|
| 23 |
+
n = int(m.group(2))
|
| 24 |
+
if r > n or r < 0:
|
| 25 |
+
return None
|
| 26 |
+
result = comb(n, r)
|
| 27 |
+
return SolverResult(
|
| 28 |
+
domain="quant",
|
| 29 |
+
solved=True,
|
| 30 |
+
topic="combinations",
|
| 31 |
+
answer_value=str(result),
|
| 32 |
+
internal_answer=str(result),
|
| 33 |
+
steps=[
|
| 34 |
+
"Use combinations when order does not matter.",
|
| 35 |
+
"Compute nCr.",
|
| 36 |
+
],
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# "committee of 3 from 8"
|
| 40 |
+
m = re.search(r"committee\s+of\s+(\d+).*?from\s+(\d+)", lower)
|
| 41 |
+
if m:
|
| 42 |
+
r = int(m.group(1))
|
| 43 |
+
n = int(m.group(2))
|
| 44 |
+
if r > n or r < 0:
|
| 45 |
+
return None
|
| 46 |
+
result = comb(n, r)
|
| 47 |
+
return SolverResult(
|
| 48 |
+
domain="quant",
|
| 49 |
+
solved=True,
|
| 50 |
+
topic="combinations",
|
| 51 |
+
answer_value=str(result),
|
| 52 |
+
internal_answer=str(result),
|
| 53 |
+
steps=[
|
| 54 |
+
"A committee is a selection problem, so use combinations.",
|
| 55 |
+
"Compute nCr.",
|
| 56 |
+
],
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# "arrange 5 books" / "permutations of 5 items"
|
| 60 |
+
m = re.search(r"(?:arrange|arrangement|permutation).*?(\d+)", lower)
|
| 61 |
+
if m and "choose" not in lower and "committee" not in lower:
|
| 62 |
+
n = int(m.group(1))
|
| 63 |
+
result = factorial(n)
|
| 64 |
+
return SolverResult(
|
| 65 |
+
domain="quant",
|
| 66 |
+
solved=True,
|
| 67 |
+
topic="permutations",
|
| 68 |
+
answer_value=str(result),
|
| 69 |
+
internal_answer=str(result),
|
| 70 |
+
steps=[
|
| 71 |
+
"When arranging all distinct items, use n!.",
|
| 72 |
+
],
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
# "how many ways to arrange r from n"
|
| 76 |
+
m = re.search(r"arrange\s+(\d+).*?from\s+(\d+)", lower)
|
| 77 |
+
if m:
|
| 78 |
+
r = int(m.group(1))
|
| 79 |
+
n = int(m.group(2))
|
| 80 |
+
if r > n or r < 0:
|
| 81 |
+
return None
|
| 82 |
+
result = perm(n, r)
|
| 83 |
+
return SolverResult(
|
| 84 |
+
domain="quant",
|
| 85 |
+
solved=True,
|
| 86 |
+
topic="permutations",
|
| 87 |
+
answer_value=str(result),
|
| 88 |
+
internal_answer=str(result),
|
| 89 |
+
steps=[
|
| 90 |
+
"Use permutations when order matters.",
|
| 91 |
+
"Compute nPr.",
|
| 92 |
+
],
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
return None
|