猿辅导第一题,菜鸡代码,欢迎指正
# 带括号的字符串解压
c = 1
for i in range(c):
a = list('A11B')
b = []
stack = []
while len(a):
# print(b,stack)
x = a.pop(0)
if x!=')' and not x.isdigit():
if x=='(':
if len(stack) and stack[-1]!='(':
b += stack
stack.append(x)
elif x==')':
nu = int(a.pop(0))
while a[0].isdigit():
nu = nu + a.pop(0)
tem = stack.pop()
while stack[-1].isalpha():
tem += stack.pop()
stack.pop()
tem = tem*nu
# print('中间',tem,stack)
if len(stack)!=0:
stack += (tem)
else:
b += (tem)
else:
nu = x
while len(a) and a[0].isdigit():
nu = nu + a.pop(0)
nu = int(nu)
# print(nu)
tem = stack.pop()
while len(stack) and stack[-1].isalpha():
tem = stack.pop()
if len(stack):
stack += tem*nu
else:
b.append(tem*nu)
stack = []
if len(stack):
b = b + stack
b = [str(x) for x in b]
# print(''.join(b))
print(''.join(b))
#猿辅导##笔试题目#
c = 1
for i in range(c):
a = list('A11B')
b = []
stack = []
while len(a):
# print(b,stack)
x = a.pop(0)
if x!=')' and not x.isdigit():
if x=='(':
if len(stack) and stack[-1]!='(':
b += stack
stack.append(x)
elif x==')':
nu = int(a.pop(0))
while a[0].isdigit():
nu = nu + a.pop(0)
tem = stack.pop()
while stack[-1].isalpha():
tem += stack.pop()
stack.pop()
tem = tem*nu
# print('中间',tem,stack)
if len(stack)!=0:
stack += (tem)
else:
b += (tem)
else:
nu = x
while len(a) and a[0].isdigit():
nu = nu + a.pop(0)
nu = int(nu)
# print(nu)
tem = stack.pop()
while len(stack) and stack[-1].isalpha():
tem = stack.pop()
if len(stack):
stack += tem*nu
else:
b.append(tem*nu)
stack = []
if len(stack):
b = b + stack
b = [str(x) for x in b]
# print(''.join(b))
print(''.join(b))
#猿辅导##笔试题目#