-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplify_path.rs
68 lines (52 loc) · 1.48 KB
/
simplify_path.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
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
// https://leetcode.com/problems/simplify-path/
pub fn simplify_path(path: String) -> String {
let elements = path.split("/");
let mut stack = vec![""];
for element in elements {
if element == ".." {
if stack.len() > 1 {
stack.pop();
};
} else if (element != "") && (element != ".") {
stack.push(element)
}
}
if stack.len() == 1 {
stack.push("");
}
stack.join("/")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn simplify_path_removes_trailing_slash() {
let actual = simplify_path("/home/".to_string());
assert_eq!(actual, "/home");
}
#[test]
fn simplify_path_handles_initial_double_period() {
let actual = simplify_path("/../".to_string());
assert_eq!(actual, "/");
}
#[test]
fn simplify_path_handles_later_double_period() {
let actual = simplify_path("/home/../".to_string());
assert_eq!(actual, "/");
}
#[test]
fn simplify_path_handles_period() {
let actual = simplify_path("/.".to_string());
assert_eq!(actual, "/");
}
#[test]
fn simplify_path_handles_multiple_slash() {
let actual = simplify_path("home//foo".to_string());
assert_eq!(actual, "/home/foo");
}
#[test]
fn simplify_path_handles_complex_input() {
let actual = simplify_path("home/../src//./test/".to_string());
assert_eq!(actual, "/src/test");
}
}