Search code examples
pythonphpoutputdata-conversion

Different output from python function and php conversion


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 ?


Solution

  • The problem is in the translation of the following Python statement:

    K.append(p+[j])
    

    p+[j] will create a new list from taking list p and appending element j, leaving the original list unmodified. Therefore, an equivalent PHP translation would be:

    $p_copy = $p;
    $p_copy[] = $j;
    $K[] = $p_copy;
    

    I also had to replace:

    foreach(range($m, $M+1) as $j){
    

    with:

    foreach(range($m, $M) as $j){
    

    Putting this all together:

    <?php
    
    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) as $j){
                    $p_copy = $p;
                    $p_copy[] = $j;
                    $K[] = $p_copy;
                }
            }
            $L = $K;
        }
        return $L;
    }
    
    print_r(sous([3,2,1,0], 2));
    

    Prints:

    Array
    (
        [0] => Array
            (
                [0] => 0
                [1] => 1
                [2] => 1
                [3] => 0
            )
    
        [1] => Array
            (
                [0] => 0
                [1] => 2
                [2] => 0
                [3] => 0
            )
    
        [2] => Array
            (
                [0] => 1
                [1] => 0
                [2] => 1
                [3] => 0
            )
    
        [3] => Array
            (
                [0] => 1
                [1] => 1
                [2] => 0
                [3] => 0
            )
    
        [4] => Array
            (
                [0] => 2
                [1] => 0
                [2] => 0
                [3] => 0
            )
    
    )