langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
5
Task/Entropy/Perl-6/entropy.pl6
Normal file
5
Task/Entropy/Perl-6/entropy.pl6
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
sub entropy(@a) {
|
||||
[+] map -> \p { p * -log p }, @a.bag.values »/» +@a;
|
||||
}
|
||||
|
||||
say log(2) R/ entropy '1223334444'.comb;
|
||||
27
Task/Entropy/XPL0/entropy.xpl0
Normal file
27
Task/Entropy/XPL0/entropy.xpl0
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
code real RlOut=48, Ln=54; \intrinsic routines
|
||||
string 0; \use zero-terminated strings
|
||||
|
||||
func StrLen(A); \Return number of characters in an ASCIIZ string
|
||||
char A;
|
||||
int I;
|
||||
for I:= 0, -1>>1-1 do
|
||||
if A(I) = 0 then return I;
|
||||
|
||||
func real Entropy(Str); \Return Shannon entropy of string
|
||||
char Str;
|
||||
int Len, I, Count(128);
|
||||
real Sum, Prob;
|
||||
[Len:= StrLen(Str);
|
||||
for I:= 0 to 127 do Count(I):= 0;
|
||||
for I:= 0 to Len-1 do \count number of each character in string
|
||||
Count(Str(I)):= Count(Str(I)) + 1;
|
||||
Sum:= 0.0;
|
||||
for I:= 0 to 127 do
|
||||
if Count(I) # 0 then \(avoid Ln(0.0) error)
|
||||
[Prob:= float(Count(I)) / float(Len); \probability of char in string
|
||||
Sum:= Sum + Prob*Ln(Prob);
|
||||
];
|
||||
return -Sum/Ln(2.0);
|
||||
];
|
||||
|
||||
RlOut(0, Entropy("1223334444"))
|
||||
Loading…
Add table
Add a link
Reference in a new issue