The Score of Students Solving Math Expression - Problem
You're a teacher grading math homework! 📝 You've given your elementary school students a simple arithmetic expression containing only single digits (0-9), addition (+), and multiplication (*) symbols.
Here's the catch: you taught them a specific order of operations:
- First: Compute all multiplications from left to right
- Then: Compute all additions from left to right
For example, with expression "3+5*2":
- Correct answer: 3 + (5*2) = 3 + 10 = 13
- Wrong order: (3+5) * 2 = 8 * 2 = 16
Now you need to grade the students' answers based on these rules:
- 5 points: Correct answer using proper order of operations
- 2 points: Wrong answer BUT could be obtained by applying operations in some different (but mathematically valid) order
- 0 points: Completely wrong answer that can't be explained by any order of operations
Goal: Return the total points earned by all students.
Input & Output
example_1.py — Basic Expression
$
Input:
s = "7+3*1*2", answers = [20, 13, 42]
›
Output:
7
💡 Note:
Correct answer: 7+(3*1*2) = 7+6 = 13. Student with answer 13 gets 5 points. Answer 20 could come from (7+3)*1*2 = 20, so 2 points. Answer 42 is impossible with any valid parenthesization, so 0 points. Total: 5+2+0 = 7
example_2.py — Simple Addition
$
Input:
s = "3+5*2", answers = [13, 0, 10, 13, 13, 16, 16]
›
Output:
19
💡 Note:
Correct answer: 3+(5*2) = 13. Three students got 13 (5×3=15 points). Two students got 16 from (3+5)*2 (2×2=4 points). Answers 0 and 10 are impossible (0×2=0 points). Total: 15+4+0 = 19
example_3.py — Single Number
$
Input:
s = "6+0*1", answers = [6, 1, 0]
›
Output:
7
💡 Note:
Correct answer: 6+(0*1) = 6+0 = 6. Student with answer 6 gets 5 points. Answer 0 could come from (6+0)*1 = 0, so 2 points. Answer 1 is impossible, so 0 points. Total: 5+2+0 = 7
Constraints
- 3 ≤ s.length ≤ 31
- s consists of digits and operators '+' and '*' only
- All the integer operands in the expression are in the range [0, 9]
- 1 ≤ answers.length ≤ 104
- 0 ≤ answers[i] ≤ 1000
- The expression is guaranteed to be valid (alternating numbers and operators)
Visualization
Tap to expand
Understanding the Visualization
1
Parse Expression
Break down '3+5*2' into numbers and operators
2
Find Correct Answer
Apply taught rules: multiply first (5*2=10), then add (3+10=13)
3
Generate All Possibilities
Use DP to find alternative valid results: (3+5)*2=16
4
Grade Students
Award 5 points for correct (13), 2 points for valid alternative (16), 0 for impossible
Key Takeaway
🎯 Key Insight: Use interval dynamic programming to efficiently compute all mathematically valid results, then award partial credit for creative but correct approaches.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code