-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmile_eliminations.py
50 lines (41 loc) · 1.54 KB
/
smile_eliminations.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
50
#
# You are given a string that may contains `smile` with variable number of brackets:
# :-(( or :-)))))
# You have to write function that clean it out from smiles
#
# a:-)()) => a())
# :-(((abc:-() => abc)
# a => a
#
def smile_eliminations(some_str):
w_idx, r_idx = 0, 0
ll = len(some_str)
end = ll - 3
while r_idx <= end:
if some_str[r_idx] == ':' and some_str[r_idx+1] == '-' and some_str[r_idx+2] in [")", "("]:
prev = some_str[r_idx+2]
r_idx += 3
while r_idx < ll and some_str[r_idx] == prev:
r_idx += 1
else:
some_str[w_idx] = some_str[r_idx]
w_idx += 1
r_idx += 1
# remainder
for rr in xrange(r_idx, ll):
some_str[w_idx] = some_str[rr]
w_idx += 1
print some_str[:w_idx]
return some_str[:w_idx]
assert smile_eliminations(list("a:-)())")) == list("a())")
assert smile_eliminations(list(":-(((abc:-()")) == list("abc)")
assert smile_eliminations(list("a")) == list("a")
assert smile_eliminations(list("abc")) == list("abc")
assert smile_eliminations(list("abc:")) == list("abc:")
assert smile_eliminations(list(":-):-(")) == list("")
assert smile_eliminations(list(":-:-(")) == list(":-")
assert smile_eliminations(list(":-(((((")) == list("")
assert smile_eliminations(list("")) == list("")
assert smile_eliminations(list(":-))))))(:-((()a(bc:-:-)")) == list("()a(bc:-")
assert smile_eliminations(list("a:-)b:-(c")) == list("abc")
assert smile_eliminations(list(":-)ca")) == list("ca")