转:用Python解答百度测试开发算法面试题

http://www.cnblogs.com/5bug/p/7684323.html

题目是:有一组“+”和“-”符号,要求将“+”排到左边,“-”排到右边,写出具体的实现方法。

方法一:

def StringSort(data):
    count=len(data)

    for j in range(count):
        for i in range(count-1):
            if data[i] == '-':
                data[i],data[i+1] = data[i+1],data[i]
                i=i+1
            else:
                i=i+1
        j=j+1
    return data

data = ['-','-','+','+','-']
print(StringSort(data))

方法二:

data = ['-','-','+','+','-']
lq=[]
length=len(data)
for i in range(length):
    if '-' in data:
        wz=data.index('-')
        lq.append(data[wz])
        del data[wz]
print(data+lq)

posted @ 2019-05-06 14:27  by2018  阅读(536)  评论(0编辑  收藏  举报