Skip to content

Commit 0eaeacc

Browse files
committed
Upsert 1807-evaluate-the-bracket-pairs-of-a-string.rs
1 parent 0faa96b commit 0eaeacc

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
pub fn evaluate(s: String, knowledge: Vec<Vec<String>>) -> String {
2+
use std::collections::HashMap;
3+
let mut mmp = HashMap::new();
4+
for pair in knowledge {
5+
mmp.insert(pair[0].clone(), pair[1].clone());
6+
}
7+
// println!("{:?}", mmp);
8+
let mut ret = String::new();
9+
let mut current_word = String::new();
10+
let mut start = false;
11+
for c in s.chars() {
12+
if c == '(' {
13+
start = true;
14+
continue
15+
}
16+
if c == ')' {
17+
start = false;
18+
let get = mmp.get(&current_word);
19+
if get == None {
20+
ret.push('?')
21+
} else {
22+
ret.push_str(get.unwrap())
23+
}
24+
current_word = String::new();
25+
continue
26+
}
27+
if start {
28+
current_word.push(c)
29+
} else {
30+
ret.push(c)
31+
}
32+
}
33+
ret
34+
}
35+
36+
fn main() {
37+
println!(
38+
"{:?}",
39+
evaluate(
40+
"(name)is(age)yearsold(asd)".to_string(),
41+
vec![
42+
vec!["name".to_string(), "bob".to_string()],
43+
vec!["age".to_string(), "two".to_string()]
44+
]
45+
)
46+
);
47+
}

0 commit comments

Comments
 (0)