from math import sqrt


def asymmetry(pi, pK, e, g_pK, g_e):
    """
    calculates background-subtracted asymmetry given
    pi   = (content,error)
    pK   = (content,error)
    e    = (content,error)
    g_pK = (pK contamination in pi window) / purity of pK sideband
    g_e  = (e  contamination in pi window) / purity of e sideband
    
    returns (content,error)
    """
    content = (pi[0] - g_pK*pK[0] - g_e*e[0]) / (1 - g_pK - g_e)
    error = sqrt(pi[1]**2 + (g_pK*pK[1])**2 + (g_e*e[1])**2) / (1 - g_pK - g_e)
    return (content,error)