我怎么能阻止这种递归呢?

时间:2017-11-23 22:41:26

标签: python-3.x recursion

我正在做一个命令奖牌表的功能。类似的事情:如果两个或更多的球队拥有相同数量的金牌,那么银牌将从最低到最低,然后是铜牌。

列表中的每个列表都是一个国家[金,银,铜]:

[[1, 2, 0], [0, 1, 0], [2, 0, 0], [0, 0, 3]]

那是回归:

[[2, 0, 0], [1, 2, 0], [0, 1, 0], [0, 0, 3]]  

我的功能正常,但我找不到停止递归的方法......

代码:

def ordem(lista,lista2,x):
    cond=0
    cond2=0
    cond3=0
    cont2=0
    for xx in lista:
        if x-cont2==1:
            break
        if lista[cont2+1][0] > lista[cont2][0]:
            lista[cont2+1],lista[cont2]=lista[cont2],lista[cont2+1]
            lista2[cont2+1],lista2[cont2]=lista2[cont2],lista2[cont2+1]
            cond=1
            cond2=1
            #print("1")
            #cont2+=1
        if lista[cont2+1][1] > lista[cont2][1] and cond2 ==0:
            lista[cont2+1],lista[cont2]=lista[cont2],lista[cont2+1]
            lista2[cont2+1],lista2[cont2]=lista2[cont2],lista2[cont2+1]
            cond=1
            cond3=1
            #print("2")
            #cont2+=1
        if lista[cont2+1][2] > lista[cont2][2] and (cond2==0 and cond3==0):
            lista[cont2+1],lista[cont2]=lista[cont2],lista[cont2+1]
            lista2[cont2+1],lista2[cont2]=lista2[cont2],lista2[cont2+1]
            cond=1
            #print("3")
            #cont2+=1


        #else: cond=False
        cont2+=1
    if cond!=1:
        #print(lista)
        return lista2

    #print(lista)
    #print("cond:"+str(cond))
    return ordem(lista,lista2,x)

我尝试将已经排序的元素添加到列表中,然后在进行切换时检查它们是否在其中,但它也无法正常工作

1 个答案:

答案 0 :(得分:1)

为什么不使用Python sorted而不是构建自己的排序算法?

from operator import itemgetter 

medals = [[1, 2, 0], [0, 1, 0], [2, 0, 0], [0, 0, 3]]
res = sorted(medals, key=itemgetter(0,1,2), reverse=True)
print (res)

输出:

[[2, 0, 0], [1, 2, 0], [0, 1, 0], [0, 0, 3]]

或者执行直接改变原始列表的排序:

medals.sort(key=itemgetter(0,1,2), reverse=True)

QuickSort实施

在评论中,您提到不允许您使用sortsorted。在这种情况下,您可以使用以下QuickSort实现:

def quick_sort(lst, cmp):
    def recurse(first, last):
        if first >= last:
            return
        pivotvalue = lst[first]
        left = first + 1
        right = last
        while True:
            while left <= right and cmp(lst[left], pivotvalue) < 0:
                left += 1
            while left <= right and cmp(lst[right], pivotvalue) > 0:
                right -= 1
            if left > right:
                break
            lst[left], lst[right] = lst[right], lst[left]
        lst[first], lst[right] = lst[right], pivotvalue
        recurse(first, right-1)
        recurse(left, last)

    recurse(0, len(lst)-1)

def medals_compare(a, b):
    if a[0] != b[0]:
        return b[0] - a[0]
    if a[1] != b[1]:
        return b[1] - a[1]
    return b[2] - a[2]

medals = [[1, 2, 0], [0, 1, 0], [2, 0, 0], [0, 0, 3]]
quick_sort(medals, medals_compare)
print(medals)
相关问题