-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2011.rs
38 lines (33 loc) · 976 Bytes
/
p2011.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
let operations1: Vec<String> = ["--X", "X++", "X++"]
.iter()
.map(|&s| s.to_string())
.collect();
assert_eq!(final_value_after_operations(operations1), 1);
let operations2: Vec<String> = ["++X", "++X", "X++"]
.iter()
.map(|&s| s.to_string())
.collect();
assert_eq!(final_value_after_operations(operations2), 3);
let operations3: Vec<String> = ["X++", "++X", "--X", "X--"]
.iter()
.map(|&s| s.to_string())
.collect();
assert_eq!(final_value_after_operations(operations3), 0);
}
}
pub fn final_value_after_operations(operations: Vec<String>) -> i32 {
let mut x = 0;
for s in operations {
match s.as_str() {
"X++" | "++X" => x += 1,
"X--" | "--X" => x -= 1,
_ => (),
}
}
x
}