-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdecodeString.py
More file actions
36 lines (31 loc) · 908 Bytes
/
decodeString.py
File metadata and controls
36 lines (31 loc) · 908 Bytes
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
from string import ascii_lowercase as letters
digits = '1234567890'
def decodeString(s):
stack = []
string = [x for x in s]
def calc(chars, mult = 1):
if stack:
temp = stack.pop()
else:
temp = ''
if temp == ']': temp = ''
stack.append(mult * chars + temp)
while string:
if string[-1] == '[':
string.pop()
continue
if string[-1] == ']':
stack.append(string.pop())
continue
if string[-1] in letters:
calc(string.pop())
continue
if string[-1] in digits:
mult = string.pop()
if string:
while string[-1] in digits:
mult = string.pop() + mult
if not string: break
calc(stack.pop(), int(mult))
continue
return(''.join(stack))