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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
pub fn strip_dag(vertex: &str, char_to_strip: &str) -> String {
let dag = vertex.split(",").collect::<Vec<&str>>();
let dag_original_length = dag.len();
let strip_pattern_1 = format!("-{}", char_to_strip);
let strip_pattern_2 = format!("{}-", char_to_strip);
let strip_pattern_3 = format!("{}", char_to_strip);
let mut new_dag: Vec<String> = Vec::with_capacity(dag_original_length);
let mut done_idx: Vec<usize> = Vec::with_capacity(dag_original_length);
let mut idx_lookahead: usize;
for (idx, vert) in dag.iter().enumerate() {
if done_idx.contains(&idx) {
continue;
}
if vert.contains(char_to_strip) {
let stripped_vertex =
strip_vertice(&strip_pattern_1, &strip_pattern_2, &strip_pattern_3, vert);
let stripped_vertice_len = stripped_vertex.len();
idx_lookahead = idx + 1;
while let Some(lookahead_vertice) = check_next_vertice(
&dag,
&idx_lookahead,
&stripped_vertice_len,
char_to_strip,
&stripped_vertex,
&strip_pattern_1,
&strip_pattern_2,
&strip_pattern_3,
) {
push_if_not_duplicate(lookahead_vertice, &mut new_dag, &idx);
done_idx.push(idx_lookahead);
idx_lookahead += 1;
}
continue;
}
push_if_not_duplicate(String::from(*vert), &mut new_dag, &idx)
}
new_dag.sort();
let formatted_dag = new_dag.join(",");
formatted_dag
}
fn push_if_not_duplicate(current_vertex: String, new_dag: &mut Vec<String>, idx: &usize) -> () {
if *idx != 0 {
if let Some(vertex) = new_dag.get(idx - 1) {
match (vertex.chars().last(), current_vertex.chars().last()) {
(Some(char1), Some(char2)) if char1 == char2 => (),
_ => new_dag.push(current_vertex),
}
}
} else {
new_dag.push(current_vertex);
}
}
fn strip_vertice(
strip_pattern_1: &String,
strip_pattern_2: &String,
strip_pattern_3: &String,
vertice: &&str,
) -> String {
vertice
.replace(&*strip_pattern_1, "")
.replace(&*strip_pattern_2, "")
.replace(&*strip_pattern_3, "")
}
fn check_next_vertice<'a>(
dag: &Vec<&str>,
idx_lookahead: &'a usize,
vertice_len: &usize,
char_to_strip: &str,
first_stripped_vertice: &String,
strip_pattern_1: &String,
strip_pattern_2: &String,
strip_pattern_3: &String,
) -> Option<String> {
if vertice_len > &1 {
None
} else {
match dag.get(*idx_lookahead) {
Some(lookahead_vertice) if lookahead_vertice.contains(char_to_strip) => {
let stripped_lookahead_vertice = strip_vertice(
strip_pattern_1,
strip_pattern_2,
strip_pattern_3,
lookahead_vertice,
);
let new_vertice = Some(format!(
"{}-{}",
first_stripped_vertice, stripped_lookahead_vertice
));
new_vertice
}
_ => None,
}
}
}
#[cfg(test)]
mod test {
use super::strip_dag;
#[test]
fn strips_dag_successfully() {
let original_dag = "a-b,b-c,c-d,c-d";
let vertice_to_strip = "d";
let expected_dag_result = "a-b,b-c";
let stripped_dag = strip_dag(original_dag, vertice_to_strip);
assert_eq!(expected_dag_result, stripped_dag);
}
#[test]
fn nothing_to_strip_just_remove_duplicates() {
let original_dag = "a-b,b-c,c-d,c-d";
let vertice_to_strip = "x";
let expected_dag_result = "a-b,b-c,c-d";
let stripped_dag = strip_dag(original_dag, vertice_to_strip);
assert_eq!(expected_dag_result, stripped_dag);
}
#[test]
fn strip_alphabetically() {
let original_dag = "a-b,b-d,d,d";
let vertice_to_strip = "c";
let expected_dag_result = "a-b,b-d";
let stripped_dag = strip_dag(original_dag, vertice_to_strip);
assert_eq!(expected_dag_result, stripped_dag);
}
}