def hamming_distance(a, expected, allow_iupac=True):
    """ 
    Compute the hamming distance allowing for IUPAC encoding wildcards 
    
    Args:
        observed (str) - the observed barcode from sequencesin ACGT space
        expected (str) - the structure of the barcode in IUPAC format
        allow_iupac (bool) - True if IUPAC format is allowed for matching, False otherwise
    Returns:
        edit_distance (int) - The number number of mismatches 

    Example:
        > hamming_distance("ACTG", "WSWS", allow_iupac=True)
        0
        > hamming_distance("ACTT", "WSWS", allow_iupac=True)
        1
        > hamming_distance("GACT", "WSWS", allow_iupac=True)
        4

    """
    if allow_iupac:
        translation = {"A":"A",
            "C":"C",
            "G":"G",
            "T":"T",
            "R":"AG",
            "Y":"CT",
            "S":"GC",
            "W":"AT",
            "K":"GT",
            "M":"AC",
            "B":"CGT",
            "D":"AGT",
            "H":"ACT",
            "V":"ACG",
            "N":"ACGT"} 
    else:
        translation = {"A":"A",
            "C":"C",
            "G":"G",
            "T":"T"}


    c = 0
    for i in range(len(a)):
        try:
            if a[i] in translation[expected[i]]:
                continue
            else:
                c+=1
        except IndexError:
            c+=len(a)-i
    return c