I try to convert the following python function
def Sous(dist,d) :
l=len(dist)
L=[[]]
for i in range(l) :
K=[]
s=sum(dist[i+1:])
for p in L :
q=sum(p)
m=max(d-q-s,0)
M=min(dist[i],d-q)
for j in range(m,M+1) :
K.append(p+[j])
L=K
return L
print(Sous([3,2,1,0],2))
into a PHP function
function sous($dist, $d){
$l = count($dist);
$L = [[]];
foreach(range(0,$l - 1) as $i){
$K = [];
$s = array_sum(array_slice($dist, $i+1));
foreach($L as $p){
$q = array_sum($p);
$m = max($d-$q-$s, 0);
$M = min($dist[$i], $d-$q);
foreach(range($m, $M+1) as $j){
$K[] = $p+[$j];
}
}
$L = $K;
}
return $L;
}
print_r(sous([3,2,1,0],2));
But as I get the right output for python function :
[[0, 1, 1, 0], [0, 2, 0, 0], [1, 0, 1, 0], [1, 1, 0, 0], [2, 0, 0, 0]]
I get a wrong output for the PHP conversion
Array
(
[0] => Array
(
[0] => 0
)
[1] => Array
(
[0] => 0
)
[2] => Array
(
[0] => 0
)
[3] => Array
(
[0] => 0
)
[4] => Array
(
[0] => 0
)
...
I can't find where is the problem in the PHP conversion, as I don't really understand the code I try to convert.
Do you have any ideas of where I did wrong ?