RosettaCodeData/Task/Entropy/PureBasic/entropy.basic

23 lines
500 B
Plaintext

#TESTSTR="1223334444"
NewMap uchar.i() : Define.d e
Procedure.d nlog2(x.d) : ProcedureReturn Log(x)/Log(2) : EndProcedure
Procedure countchar(s$, Map uchar())
If Len(s$)
uchar(Left(s$,1))=CountString(s$,Left(s$,1))
s$=RemoveString(s$,Left(s$,1))
ProcedureReturn countchar(s$, uchar())
EndIf
EndProcedure
countchar(#TESTSTR,uchar())
ForEach uchar()
e-uchar()/Len(#TESTSTR)*nlog2(uchar()/Len(#TESTSTR))
Next
OpenConsole()
Print("Entropy of ["+#TESTSTR+"] = "+StrD(e,15))
Input()