Shannon entropy

Write codeMedium18 minMeta style

Your task

Compute the Shannon entropy (in bits, base-2) of a list of class labels. Entropy = -sum over each class of p * log2(p), where p is the class's fraction. By convention a class contributing p = 0 adds nothing, and a pure single-class list has entropy 0. The input list is non-empty. Round the result to 6 decimal places.

Examples

entropy(labels: list[str]) -> float
Example 1
Inputentropy(["a", "a", "b", "b"])
Output1