"""
Created on: Wed Nov 23 13:27:59 2022
Author: vila@cs.upc.edu
"""
def update_standings(Ds, Lr):
    for e1, e2, g1, g2 in Lr:
        if g1 > g2:
            update_team(Ds, e1, 1, 0, 0, g1, g2, 3)
            update_team(Ds, e2, 0, 0, 1, g2, g1, 0)
        elif g1 == g2:
            update_team(Ds, e1, 0, 1, 0, g1, g2, 1)
            update_team(Ds, e2, 0, 1, 0, g2, g1, 1)
        else: # g1 < g2
            update_team(Ds, e1, 0, 0, 1, g1, g2, 0)
            update_team(Ds, e2, 1, 0, 0, g2, g1, 3)
            
def update_team(D, e, w, d, l, gf, ga, p):
    if e not in D:
        D[e] = [1, w, d, l, gf, ga, p, float(p)]
    else:
        D[e][0] += 1
        D[e][1] += w
        D[e][2] += d
        D[e][3] += l
        D[e][4] += gf
        D[e][5] += ga
        D[e][6] += p
        D[e][7] = round(D[e][6] / D[e][0], 1)
        
def print_standings(Ds):
    for k in Ds:
        print([k], ' : ', Ds[k])

def list_standings(Ds):
    l = list(Ds.items())
    l.sort(key= lambda x: (-x[1][6], -(x[1][4]-x[1][5])))
    return l

def insert_gd(Ds):
    for l in Ds.values():
        l.insert(-2, l[4]-l[5])
        

def print_list(Ls):
    for l in Ls:
        print(l[0], ': ', l[1])

'''
def comprova(l):
    country, p, w, d, l, pts, ppg, gf, ga, last5 = l
    return p == w+d+l and pts == w*3 + d*1 and ppg == round(pts/p, 1) and suma_punts(last5) == pts

def suma_punts(l):
    s = 0
    for e in l:
        if e != -1:
            s += e
    return s
'''

"""
if __name__ == '__main__':
    import doctest
    doctest.testmod(verbose=True)
"""
