taynan
taynan

Reputation: 77

How can i stop this recursion?

I'm doing a function that orders a table of medals. Something like: If two or more teams have the same number of gold medals, the silver medals are then judged from the most to the least and then the bronze medals.

Each list in the list is a country [gold,silver,bronze]:

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

That's return:

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

My function is working, but i cant find a way to stop the recursion...

Code:

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)

I tried to add the elements that have already been sorted into a list and then check if they are in it at the time of making the switch, but it also did not work

Upvotes: 0

Views: 41

Answers (1)

trincot
trincot

Reputation: 350750

Why not use Python sorted instead of building your own sorting algorithm?

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)

Outputs:

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

Or to perform the sort directly mutating the original list:

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

QuickSort implementation

In comments you mention that you are not allowed to use sort or sorted. In that case you can use the following QuickSort implementation:

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)

Upvotes: 1

Related Questions