"""
Created on Fri Jan  9 17:18:31 2026
@author: Lluís Vila
(c) 2025-26
"""
def count_pass_fail(clrL, stgD):
    passfailL = []
    errL = [] 
    for i, row in enumerate(clrL):
        npass, nfail, rowerrL = count_pass_fail_row(row, stgD)
        # print(f'isok {isok}, res {res}, val {val}')
        passfailL.append( (i+1, npass, nfail) )
        errL.extend(rowerrL)
    errL.sort()
    return passfailL, errL

def count_pass_fail_row(row, grdD):
    npass = 0
    nfail = 0
    errL = []
    n = 0
    for stid in row:
        # print(f'i {i}, stid {stid}')
        if stid == -1:
            pass
        elif stid not in grdD: # invalid student id
            errL.append(stid)
        else: # there is a valid student id
            g = grdD[stid][1]
            if g >= 5.0:
                npass += 1
            else:
                nfail += 1
    return npass, nfail, errL
