-
Notifications
You must be signed in to change notification settings - Fork 0
/
Coding Challenge.py
49 lines (38 loc) · 1.19 KB
/
Coding Challenge.py
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
from re import sub
def normalize(path):
"""Normalize a file path.
All single dot components of the path must be removed. For
example, "foo/./bar" should be normalized to "foo/bar".
All double dots components of the path must be removed,
along with their parent directory, if any. For example,
"foo/bar/../baz" should be normalized to "foo/baz".
>>> normalize("foo/./bar")
'foo/bar'
>>> normalize("foo/bar/../baz")
'foo/baz'
>>> normalize("foo//bar")
'foo//bar'
>>> normalize("/..")
''
>>> normalize("/../")
'/'
>>> normalize("foo/..")
''
#No-ops
>>> normalize(".bar")
'.bar'
>>> normalize("/..bar")
'/..bar'
>>> normalize("/..bar/")
'/..bar/'
"""
#Remove double-dot components
path = sub('/[^/]+/\.\.(?=/|$)', '', path) #Match '/bar/..'
path = sub('([^/]+/)?\.\./', '', path) #Match 'bar/../' or beginning '../'
path = sub('^[^/]*/?\.\.$', '', path) #Match beginning 'bar/..' and '..'
#Remove single-dot components
path = sub('(?=/|^)\./', '', path) #Match ./
path = sub('/\.(?=/|$)', '', path) #Match /.
return path
while True:
print(normalize(input()))