Skip to content

Commit 93563c2

Browse files
committed
2019. The Score of Students Solving Math Expression: RETRY
1 parent 1017297 commit 93563c2

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,3 +1522,4 @@ mod s2014_longest_subsequence_repeated_k_times;
15221522
mod s2016_maximum_difference_between_increasing_elements;
15231523
mod s2017_grid_game;
15241524
mod s2018_check_if_word_can_be_placed_in_crossword;
1525+
mod s2019_the_score_of_students_solving_math_expression;
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* [2019] The Score of Students Solving Math Expression
3+
*
4+
* You are given a string s that contains digits 0-9, addition symbols '+', and multiplication symbols '*' only, representing a valid math expression of single digit numbers (e.g., 3+5*2). This expression was given to n elementary school students. The students were instructed to get the answer of the expression by following this order of operations:
5+
* <ol>
6+
* Compute multiplication, reading from left to right; Then,
7+
* Compute addition, reading from left to right.
8+
* </ol>
9+
* You are given an integer array answers of length n, which are the submitted answers of the students in no particular order. You are asked to grade the answers, by following these rules:
10+
*
11+
* If an answer equals the correct answer of the expression, this student will be rewarded 5 points;
12+
* Otherwise, if the answer could be interpreted as if the student applied the operators in the wrong order but had correct arithmetic, this student will be rewarded 2 points;
13+
* Otherwise, this student will be rewarded 0 points.
14+
*
15+
* Return the sum of the points of the students.
16+
*
17+
* Example 1:
18+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/09/17/student_solving_math.png" style="width: 678px; height: 109px;" />
19+
* Input: s = "7+3*1*2", answers = [20,13,42]
20+
* Output: 7
21+
* Explanation: As illustrated above, the correct answer of the expression is 13, therefore one student is rewarded 5 points: [20,<u>13</u>,42]
22+
* A student might have applied the operators in this wrong order: ((7+3)*1)*2 = 20. Therefore one student is rewarded 2 points: [<u>20</u>,13,42]
23+
* The points for the students are: [2,5,0]. The sum of the points is 2+5+0=7.
24+
*
25+
* Example 2:
26+
*
27+
* Input: s = "3+5*2", answers = [13,0,10,13,13,16,16]
28+
* Output: 19
29+
* Explanation: The correct answer of the expression is 13, therefore three students are rewarded 5 points each: [<u>13</u>,0,10,<u>13</u>,<u>13</u>,16,16]
30+
* A student might have applied the operators in this wrong order: ((3+5)*2 = 16. Therefore two students are rewarded 2 points: [13,0,10,13,13,<u>16</u>,<u>16</u>]
31+
* The points for the students are: [5,0,0,5,5,2,2]. The sum of the points is 5+0+0+5+5+2+2=19.
32+
*
33+
* Example 3:
34+
*
35+
* Input: s = "6+0*1", answers = [12,9,6,4,8,6]
36+
* Output: 10
37+
* Explanation: The correct answer of the expression is 6.
38+
* If a student had incorrectly done (6+0)*1, the answer would also be 6.
39+
* By the rules of grading, the students will still be rewarded 5 points (as they got the correct answer), not 2 points.
40+
* The points for the students are: [0,0,5,0,0,5]. The sum of the points is 10.
41+
*
42+
*
43+
* Constraints:
44+
*
45+
* 3 <= s.length <= 31
46+
* s represents a valid expression that contains only digits 0-9, '+', and '*' only.
47+
* All the integer operands in the expression are in the inclusive range [0, 9].
48+
* 1 <= The count of all operators ('+' and '*') in the math expression <= 15
49+
* Test data are generated such that the correct answer of the expression is in the range of [0, 1000].
50+
* n == answers.length
51+
* 1 <= n <= 10^4
52+
* 0 <= answers[i] <= 1000
53+
*
54+
*/
55+
pub struct Solution {}
56+
57+
// problem: https://leetcode.com/problems/the-score-of-students-solving-math-expression/
58+
// discuss: https://leetcode.com/problems/the-score-of-students-solving-math-expression/discuss/?currentPage=1&orderBy=most_votes&query=
59+
60+
// submission codes start here
61+
62+
impl Solution {
63+
pub fn score_of_students(s: String, answers: Vec<i32>) -> i32 {
64+
0
65+
}
66+
}
67+
68+
// submission codes end
69+
70+
#[cfg(test)]
71+
mod tests {
72+
use super::*;
73+
74+
#[test]
75+
#[ignore]
76+
fn test_2019_example_1() {
77+
let s = "7+3*1*2".to_string();
78+
let answers = vec![20, 13, 42];
79+
80+
let result = 7;
81+
82+
assert_eq!(Solution::score_of_students(s, answers), result);
83+
}
84+
85+
#[test]
86+
#[ignore]
87+
fn test_2019_example_2() {
88+
let s = "3+5*2".to_string();
89+
let answers = vec![13, 0, 10, 13, 13, 16, 16];
90+
91+
let result = 19;
92+
93+
assert_eq!(Solution::score_of_students(s, answers), result);
94+
}
95+
96+
#[test]
97+
#[ignore]
98+
fn test_2019_example_3() {
99+
let s = "6+0*1".to_string();
100+
let answers = vec![12, 9, 6, 4, 8, 6];
101+
102+
let result = 10;
103+
104+
assert_eq!(Solution::score_of_students(s, answers), result);
105+
}
106+
}

0 commit comments

Comments
 (0)