-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path001.swift
39 lines (32 loc) · 831 Bytes
/
001.swift
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
func standardizePath(_ path: String) -> String {
var result = [String]()
for component in path.split(separator: "/") {
switch component {
case "..":
guard !result.isEmpty else { return "illegal usage of .." }
result.removeLast()
case ".":
continue
default:
result.append(String(component))
}
}
return (path.hasPrefix("/") ? "/" : "") + result.joined(separator: "/") + "/"
}
func runTests() {
assert(standardizePath("/usr/bin/../bin/./scripts/../") == "/usr/bin/")
print("Tests passed.")
}
func runInteractive() {
while true {
if let value = readLine() {
let path = standardizePath(value)
print(path)
}
}
}
func main() {
runTests()
runInteractive()
}
main()