目录

python改写成php,将Python代码转换为PHP

目录

python改写成php,将Python代码转换为PHP

我创建了一个名为py2php的python-to-php转换器。它可以自动翻译基本逻辑,然后你将需要调整图书馆调用等仍然是实验性的。

下面是从OP提供的python中自动生成的PHP<?php

require_once(‘py2phplib.php’);

require_once( ‘math.php’);

function calcNumEntropyBits($s) {

if ((count($s) <= 0)) {

return 0.0;

}

$symCount = array();

foreach( $s as $c ) {

if (!$symCount.contains($c)) {

$symCount[$c] = 1;

}

else {

$symCount[$c] += 1;

}

}

$entropy = 0.0;

foreach( $symCount->iteritems() as $temp_c ) {

$prob = ($n / float(count($s)));

$entropy += ($prob * (math::log($prob) / math::log(2)));

}

if (($entropy >= 0.0)) {

return 0.0;

}

else {

return -($entropy * count($s));

}

}

function testEntropy($s) {

pyjslib_printFunc(sprintf(‘Bits of entropy in '%s' is %.2f’, new pyjslib_Tuple([$s, calcNumEntropyBits($s)])));

}

testEntropy(‘hello world’);

testEntropy(‘bubba dubba’);

testEntropy(‘aaaaaaaaaaa’);

testEntropy(‘aaaaabaaaaa’);

testEntropy(‘abcdefghijk’);

由于数学导入和包含,它将无法正确运行,但这些将很容易手工修复。