def search_first_allpassrow(gradesD, classroomL):
    for i, row in enumerate(classroomL):
        if check_allpassrow(gradesD, row):
            return i+1, row
    return -1, []

def check_allpassrow(gradesD, row):
    sol = False
    for eid in row:
        if eid != -1:
            if gradesD[eid][1] < 5.0:
                return False
            else:
                sol = True
    return sol

def search_first_allpassrow_plus(gradesD, classroomL):
    for i, row in enumerate(classroomL):
        gL = get_allpassrow_grades(gradesD, row)
        if gL != []:
            return i+1, gL
    return -1, []

def get_allpassrow_grades(gradesD, row):
    gL = []
    for eid in row:
        if eid != -1:
            g = gradesD[eid][1]
            if g < 5.0:
                return []
            else:
                gL.append(g)
    return gL

# ------------------------------------------------------

def gen_first_exc_L(gradesD, classroomL):
    L = []
    for row in classroomL:
        L.append(row_first_exc(row, gradesD))
    return L

def row_first_exc(row, gradesD):
    for i in range(len(row)):
        eid = row[i]
        if eid != -1:
            g = gradesD[eid][1]
            if g >= 9.0:
                return eid, i, g
    return -1, -1, -1.0

# ------------------------------------------------------

def gen_close_front_L(gradesD, classroomL, diff):
    closeL = []
    for irow in range(1, len(classroomL)):
        closeL.extend(row_close_front_L(gradesD, classroomL, irow, diff))
    return closeL

def row_close_front_L(gradesD, classroomL, irow, diff):
    suspL = []
    rowlen = len(classroomL[0])
    for i in range(rowlen):
        eid = classroomL[irow][i]
        if eid != -1:
            eidfront = classroomL[irow-1][i]
            if eidfront != -1:
                g = gradesD[eid][1]
                gfront = gradesD[eidfront][1]
                if abs(g - gfront) <= diff:
                    suspL.append( (gradesD[eid][0], eid, irow, i, g, eidfront, gfront) )
    return suspL
