Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

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 ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)

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
        )

)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
...