j-js commited on
Commit
abb4ceb
·
verified ·
1 Parent(s): f18b24c

Create solver_remainder.py

Browse files
Files changed (1) hide show
  1. solver_remainder.py +66 -0
solver_remainder.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_remainder(text: str) -> Optional[SolverResult]:
10
+ lower = (text or "").lower()
11
+
12
+ if "remainder" not in lower and "mod" not in lower:
13
+ return None
14
+
15
+ # "remainder when 17 is divided by 5"
16
+ m = re.search(
17
+ r"remainder.*?when\s+(-?\d+)\s+(?:is\s+)?divided\s+by\s+(-?\d+)",
18
+ lower,
19
+ )
20
+ if not m:
21
+ m = re.search(
22
+ r"(-?\d+)\s*(?:mod|%)\s*(-?\d+)",
23
+ lower,
24
+ )
25
+
26
+ if m:
27
+ a = int(m.group(1))
28
+ b = int(m.group(2))
29
+ if b == 0:
30
+ return None
31
+ result = a % b
32
+ return SolverResult(
33
+ domain="quant",
34
+ solved=True,
35
+ topic="remainder",
36
+ answer_value=str(result),
37
+ internal_answer=str(result),
38
+ steps=[
39
+ "Use the division algorithm.",
40
+ "The remainder is what is left after dividing by the divisor.",
41
+ ],
42
+ )
43
+
44
+ # "When n is divided by 5 the remainder is 2. What is remainder when 3n is divided by 5?"
45
+ m = re.search(
46
+ r"remainder\s+is\s+(\d+).*?what\s+is\s+the\s+remainder\s+when\s+(\d+)n\s+is\s+divided\s+by\s+(\d+)",
47
+ lower,
48
+ )
49
+ if m:
50
+ r = int(m.group(1))
51
+ mult = int(m.group(2))
52
+ mod = int(m.group(3))
53
+ result = (mult * r) % mod
54
+ return SolverResult(
55
+ domain="quant",
56
+ solved=True,
57
+ topic="remainder",
58
+ answer_value=str(result),
59
+ internal_answer=str(result),
60
+ steps=[
61
+ "Replace n by its remainder class modulo the divisor.",
62
+ "Multiply the remainder and reduce again modulo the divisor.",
63
+ ],
64
+ )
65
+
66
+ return None