"""
Created on Wed Aug 28 09:51:02 2019
@author: QinLong
给定一组四则运算(+ - * /),只能相邻数字相交换,求不改变运算结果的最小顺序组合。
eg:
input: 3 + 2 + 1 * 6 * -2 / 5
out : 2 + 3 + -2 * 1 * 6 / 5
"""
number = [3,2,1,6,-2,5]
n = len(number)
fuhao = ['+','+','*','*','/']
res = []
count = 0
for j in range(len(fuhao)):
if fuhao[j] == '*':
count = j
count_2 = 1
while count < n-2 and fuhao[count + 1] == '*':
count += 1
count_2 += 1
if j > 0:
if fuhao[j-1] == '/':
j +=1
tem = number[j:count+2]
tem.sort()
for k in range(len(tem)):
number[j+k] = tem[k]
print(number)
elif fuhao[j] == '/' :
count = j
count_2 = 1
while count < n-2 and fuhao[count + 1] == '/':
count += 1
count_2 += 1
if count_2 > 1:
tem = number[j+1:count+2]
tem.sort()
for k in range(len(tem)):
number[j+k+1] = tem[k]
print(number)
elif fuhao[j] == '+':
count = j
count_2 = 1
while count < n-2 and fuhao[count + 1] == '+':
count += 1
count_2 += 1
if j > 0:
if fuhao[j-1] == '*' or fuhao[j-1] == '/':
j +=1
if count <n-2:
if fuhao[count+1] == '*' or fuhao[count+1] == '/':
count -= 1
tem = number[j:count+2]
tem.sort()
for k in range(len(tem)):
number[j+k] = tem[k]
print(number)
else:
count = j
count_2 = 1
while count < n-2 and fuhao[count + 1] == '-':
count += 1
count_2 += 1
if j > 0:
if fuhao[j-1] == '*' or fuhao[j-1] == '/':
j +=1
if count <n-2:
if fuhao[count + 1] == '*' or fuhao[count + 1] == '/':
count -= 1
if count_2 > 1:
tem = number[j+1:count+2]
tem.sort()
for k in range(len(tem)):
number[j+k+1] = tem[k]
print(number)
for f in range(n-1):
res.append(number[f])
res.append(fuhao[f])
res.append(number[-1])
for r in res:
print(r, end=' ')