How to resolve the algorithm Entropy/Narcissist step by step in the Picat programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Entropy/Narcissist step by step in the Picat programming language

Table of Contents

Problem Statement

Write a computer program that computes and shows its own   entropy.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Entropy/Narcissist step by step in the Picat programming language

Source code in the picat programming language

entropy(File) = E =>
    Bytes = read_file_bytes(File),
    F = [0: I in 1..256],
    foreach (B in Bytes)
        B1 := B + 1,
        F[B1] := F[B1] + 1
    end,
    HM = 0,
    foreach (C in F)
        if (C > 0) then
            HM := HM + C * log(2, C)
        end
    end,
    L = Bytes.length,
    E = log(2, L) - HM / L.

main(Args) =>
    printf("Entropy: %f\n", entropy(Args[1])).

  

You may also check:How to resolve the algorithm Empty string step by step in the Scheme programming language
You may also check:How to resolve the algorithm String matching step by step in the CoffeeScript programming language
You may also check:How to resolve the algorithm Hello world/Line printer step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Find the missing permutation step by step in the ERRE programming language
You may also check:How to resolve the algorithm Levenshtein distance step by step in the Ela programming language