import math


def get_char_distance_distribution(charset):
    """
    Calculates the distribution of the distance between characters. VERY slow, to be used with @Memoized
    for good performance (if only a predetermined set of charsets is needed)

    :param charset: the charset as a string
    :return: a (normalized) histogram with the distribution of distance
    :rtype: dict
    """
    if len(charset) <= 1:
        return {0: 1}
    buckets = {}
    counter = 0
    for i in range(0, len(charset)):
        for j in range(0, len(charset)):
            index = int(math.fabs(ord(charset[i]) - ord(charset[j])))
            buckets[index] = buckets.get(index, 0) + 1
            counter += 1
    # normalize histogram
    for key in buckets.keys():
        buckets[key] = buckets[key] / counter

    return buckets