1. 程式人生 > >python算法習題1

python算法習題1

pan def 方法 log == return AR index pri

題目:有一組“+”和“-”符號,要求將“+”排到左邊,“-”排到右邊,寫出具體的實現方法

 1 def StringSort(data):
 2     startindex = 0
 3     endindex = 0
 4     count = len(data)
 5     while startindex + endindex < count:
 6         if data[startindex] == -:
 7             data[startindex] , data[count - endindex -1] = data[count - endindex - 1] ,data[startindex]
8 endindex += 1 9 else: 10 startindex += 1 11 return data 12 13 data = [-,-,+,-,+,+,-,-] 14 print(StringSort(data))

得到的結果:

[‘+‘, ‘+‘, ‘+‘, ‘-‘, ‘-‘, ‘-‘, ‘-‘, ‘-‘]

python算法習題1