import math
from typing import Counter


def shannon_entropy(seq):
    """
    Return the Shannon entropy of a `seq` sequence of items (typically a
    byte string).
    The entropy can be seen as the number of bits that would be required
    on average to encode seq optimally.
    See https://en.wikipedia.org/wiki/Entropy_(information_theory)
    See http://www.onlamp.com/pub/a/php/2005/01/06/entropy.html
    """

    if not seq:
        return 0.0

    log = math.log
    length = len(seq)
    frequencies = Counter(seq)
    probabilities = (freq / length for freq in frequencies.values())
    return -sum(p * log(p, 2) for p in probabilities)