Change the input string as a list
2. Handle "X" first, if find an "X", combine its previous number a and its next number b as "(a X b)".
3. Handle "+" and "-", Add each odd place as "n)", and count ")" as c.
4. Add c "(" at the first number "a" as "((((..a"
5. Output result as [:-1]
n = int(input())
for i in range(n):
ss = input().split()
while "X" in ss:
pos = ss.index("X")
state = "(" + " ".join(ss[pos-1:pos+2]) + ")"
del ss[pos]
del ss[pos]
del ss[pos-1]
ss = ss[:pos-1] + [state] + ss[pos-1:]
sslen = len(ss)//2+1
for j in range(1, sslen):
ss[2*j] += ")"
ss[0] = "("*(len(ss)//2-1) + ss[0]
if len(ss)//2-1 < 0:
ss[0] = ss[0][1-len(ss)//2:]
ans = " ".join(ss)
print(ans[:-1])
Comentários