Family Day update
This commit is contained in:
parent
aac6731f2c
commit
9ad63ea473
2442 changed files with 39761 additions and 8255 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