'''
Author: Lluís Vila
ETSEIB - UPC
c 2025-26 
'''
def list_avg_by_row(clrL, stgD):
    avgL = []
    errL = [] 
    for i, row in enumerate(clrL):
        isok, res, val = compute_avg(row, stgD)
        # print(f'isok {isok}, res {res}, val {val}')
        if isok:
            avgL.append( (i+1, res, val) )
        else:
            errL.append( (i+1, res, val) )
    return avgL, errL

def compute_avg(row, grdD):
    add = 0
    n = 0
    for i, stid in enumerate(row):
        # print(f'i {i}, stid {stid}')
        if stid == -1:
            pass
        elif stid not in grdD:
            return False, i+1, stid
        else: # there is a valid student id
            add += grdD[stid][1]
            n += 1
    if n == 0:
        return True, -1.0, 0
    else:
        return True, round(add/n, 1), n
