def extrems(lcot):
    lres = []
    for i in range (2,len(lcot) - 1):
        empresa = lcot[0]
        lant = lcot[1:i]
        lpost = lcot[i + 1:]
        mitj_ant = sum(lant)/len(lant)
        mitj_post = sum(lpost)/len(lpost)
        if lcot[i - 1] < lcot [i] > lcot[i + 1]:
            if lcot[i] > mitj_ant and lcot[i] > mitj_post:
                lres.append((empresa, 'max',lcot[i]))
        elif lcot [i - 1] > lcot[i] < lcot[i + 1]:
            if lcot[i] < mitj_ant and lcot[i] < mitj_post:
                lres.append((empresa, 'min',lcot[i]))              
    return lres

def tendencia(lemp):
    lres = []
    for lcot in lemp:
        empresa = lcot[0]
        lextrems = extrems(lcot)
        if len(lextrems) < 2:
            tend = 'indefinida'
        else:
            if lextrems[-1][2] >= lextrems[0][2]:
                tend = 'alcista'
            elif lextrems[-1][2] < lextrems[0][2]:
                tend = 'baixista'
        lres.append((empresa,tend))
    return lres
