Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
27
Task/Entropy/PHP/entropy.php
Normal file
27
Task/Entropy/PHP/entropy.php
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
function shannonEntropy($string) {
|
||||
$h = 0.0;
|
||||
$len = strlen($string);
|
||||
foreach (count_chars($string, 1) as $count) {
|
||||
$h -= (double) ($count / $len) * log((double) ($count / $len), 2);
|
||||
}
|
||||
return $h;
|
||||
}
|
||||
|
||||
$strings = array(
|
||||
'1223334444',
|
||||
'1225554444',
|
||||
'aaBBcccDDDD',
|
||||
'122333444455555',
|
||||
'Rosetta Code',
|
||||
'1234567890abcdefghijklmnopqrstuvwxyz',
|
||||
);
|
||||
|
||||
foreach ($strings AS $string) {
|
||||
printf(
|
||||
'%36s : %s' . PHP_EOL,
|
||||
$string,
|
||||
number_format(shannonEntropy($string), 6)
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue