j-js commited on
Commit
08fd06d
·
verified ·
1 Parent(s): cf11a47

Update solver_factorial.py

Browse files
Files changed (1) hide show
  1. solver_factorial.py +28 -13
solver_factorial.py CHANGED
@@ -9,15 +9,26 @@ from models import SolverResult
9
 
10
  def solve_factorial(text: str) -> Optional[SolverResult]:
11
  raw = text or ""
12
- lower = raw.lower()
13
 
14
- if "!" not in raw and "factorial" not in lower and "trailing zero" not in lower:
 
 
 
 
 
 
 
15
  return None
16
 
17
  # trailing zeros in n!
18
- m = re.search(r"trailing zeros?.*?(\d+)\s*!", lower)
19
- if m:
20
- n = int(m.group(1))
 
 
 
 
21
  count = 0
22
  power = 5
23
  while power <= n:
@@ -28,28 +39,32 @@ def solve_factorial(text: str) -> Optional[SolverResult]:
28
  domain="quant",
29
  solved=True,
30
  topic="factorial_trailing_zeros",
31
- answer_value=str(count),
32
  internal_answer=str(count),
33
  steps=[
34
  "Trailing zeros come from factors of 10.",
35
- "Each 10 contributes one 2 and one 5.",
36
- "In factorials, 5s are the limiting factor, so count factors of 5.",
 
37
  ],
38
  )
39
 
40
  # direct factorial value
41
- m = re.search(r"(\d+)\s*!", raw)
42
- if m:
43
- n = int(m.group(1))
44
  result = math.factorial(n)
 
45
  return SolverResult(
46
  domain="quant",
47
  solved=True,
48
  topic="factorial",
49
- answer_value=str(result),
50
  internal_answer=str(result),
51
  steps=[
52
- "Expand the factorial as descending multiplication.",
 
 
53
  ],
54
  )
55
 
 
9
 
10
  def solve_factorial(text: str) -> Optional[SolverResult]:
11
  raw = text or ""
12
+ lower = raw.lower().strip()
13
 
14
+ if not raw:
15
+ return None
16
+
17
+ has_factorial_notation = re.search(r"(\d+)\s*!", raw) is not None
18
+ has_factorial_word = "factorial" in lower
19
+ has_trailing_zero_phrase = "trailing zero" in lower or "trailing zeros" in lower
20
+
21
+ if not (has_factorial_notation or has_factorial_word or has_trailing_zero_phrase):
22
  return None
23
 
24
  # trailing zeros in n!
25
+ trailing_match = re.search(
26
+ r"trailing zeros?.*?(?:in|of)?\s*(\d+)\s*!",
27
+ lower
28
+ )
29
+ if trailing_match:
30
+ n = int(trailing_match.group(1))
31
+
32
  count = 0
33
  power = 5
34
  while power <= n:
 
39
  domain="quant",
40
  solved=True,
41
  topic="factorial_trailing_zeros",
42
+ answer_value=None,
43
  internal_answer=str(count),
44
  steps=[
45
  "Trailing zeros come from factors of 10.",
46
+ "Each factor of 10 is made from one 2 and one 5.",
47
+ "In a factorial, there are usually more 2s than 5s, so count the number of factors of 5.",
48
+ "Count multiples of 5, then add extra 5s from numbers like 25, 125, and so on.",
49
  ],
50
  )
51
 
52
  # direct factorial value
53
+ factorial_match = re.search(r"(\d+)\s*!", raw)
54
+ if factorial_match:
55
+ n = int(factorial_match.group(1))
56
  result = math.factorial(n)
57
+
58
  return SolverResult(
59
  domain="quant",
60
  solved=True,
61
  topic="factorial",
62
+ answer_value=None,
63
  internal_answer=str(result),
64
  steps=[
65
+ "A factorial means multiply descending whole numbers from that number down to 1.",
66
+ "Write the expression as repeated multiplication.",
67
+ "Then simplify carefully step by step.",
68
  ],
69
  )
70