题目:有一组“+”和“-”符号,要求将“+”排到左边,“-”排到右边,写出具体的实现方法
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))
得到的结果:
[‘+‘, ‘+‘, ‘+‘, ‘-‘, ‘-‘, ‘-‘, ‘-‘, ‘-‘]