-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 13 (decodeString).py3
40 lines (39 loc) · 1.01 KB
/
Day 13 (decodeString).py3
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
def multiplyString(s, n):
final_string=""
within_brackets=False
substring=""
levels=0
next_n=""
for l in s:
if l.isdigit():
if not within_brackets:
next_n+=l
else:
substring+=l
elif l=="[":
levels+=1
if levels>1:
substring+=l
if not within_brackets:
within_brackets=True
next_n=int(next_n)
elif l=="]":
levels-=1
#print(levels)
#print(substring)
#print(final_string)
if levels==0:
final_string+=multiplyString(substring, next_n)
substring=""
within_brackets=False
next_n=""
else:
substring+=l
elif within_brackets:
substring+=l
else:
final_string+=l
final_string=final_string*n
return final_string
def decodeString(s):
return multiplyString(s,1)