Shannon Entropy

What is your hint or solution suggestion?

Shannon entropy (derived from Maxwell-Boltzmann thermodynamics for information theory; see Wiki page here: ) for any given string contains a probability function,

(P(x_i, N) = ( (given value of x_i ) / (N = str.length) ) ).

Entropy can be expressed in the formula below, where x_i is the no. of distinct types of alphanumeric character, up to position i. Any such iterator loop that can pass characters (specific elements) to another blank array can be used (Foreach loop, while loop or a for loop). Using the charAt(index) function/ forEach element accessor, one could subsequently calculate the length of an array comprised of unique alphanumeric characters for any given string or substring therein, and subsequently calculate the sum of H_base2 from all such states (x_i), as a consequence.

H_base2 (X = sum(i=1, n= no. unique characters in string s != N, x_i)) = P(x_i)*[ log_e(1/P(x_i)) / log_e(2)].

This is derived through the definition of probability, and the change of base formula from the log function for Math.log(). For count_i = x_i, one could construct a probability generating function, a function for calculating n, and then use those functions to solve for H_base2. After that, it is a matter of formatting and “promised” function value passing. Unrelated, the complexity of such an algorithm passing successfully would approach O(n^3) in terms of mathematical complexity.

Challenge: Entropy

Link to the challenge:

Finish Rosetta Code: Letter Frequency first so you can reuse it to get a list of unique characters in the string and the number of times each character occurs.

Figure out how to compute a base-2 logarithm.

You can read the Wikipedia article for more information if you like, but there is sufficient information in the formula attached to the challenge. It says sum over the different unique characters in the string, multiplying the ratio of the current character count in the string and the total number of characters in the string times the base two logarithm of the same ratio, and then multiply by -1. For the ‘01’ example, there are two unique characters (N=2), each with a count of 1 (or counti = 1). So the sum becomes -1 * ((1/2) log_2(1/2) + (1/2) log_2(1/2)). Since log_2(1/2) is -1, this simplifies to -1 * (-(1/2) + -(1/2))=1. There will be a term in the sum corresponding to each unique character in the given string.