37,741
社区成员




def convert_parens(tokens):
stack = [[]]
for token in tokens:
if token == '( ': # push
sublist = []
stack[-1].append(sublist)
stack.append(sublist)
elif token == ') ': # pop
stack.pop()
else: # update top of stack
stack[-1].append(token)
return stack[0]
#!/usr/bin/python
def convert_parens(tokens):
stack = [[]]
for token in tokens:
if token == '( ': # push
sublist = []
stack[-1].append(sublist)
stack.append(sublist)
elif token == ') ': # pop
stack.pop()
else: # update top of stack
stack[-1].append(token)
return stack[0]
phrase = "( the cat ) ( sat ( on ( the mat ) ) )"
print convert_parens(phrase.split())