Python - Codigo PHP a Python

 
Vista:

Codigo PHP a Python

Publicado por LUIS ALFONSO (1 intervención) el 10/10/2019 00:27:35
Buenas tardes,

Alguien me puede ayudar pasando este código PHP a Python?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
 
//PHP program to find number of cells a queen can move 
// with obstacles on the chessborad 
 
// Return the number of position a Queen can move. 
 
function numberofPosition($n, $k, $x, $y,
                    $obstPosx, $obstPosy)
{
    // d11, d12, d21, d22 are for diagnoal distances. 
    // r1, r2 are for vertical distance. 
    // c1, c2 are for horizontal distance. 
 
    $d11;
    $d12;
    $d21;
    $d22;
    $r1;
    $r2;
    $c1;
    $c2;
 
    // Initialise the distance to end of the board. 
 
    $d11 = min( $x-1, $y-1 );
    $d12 = min( $n-$x, $n-$y );
    $d21 = min( $n-$x, $y-1 );
    $d22 = min( $x-1, $n-$y );
 
    $r1 = $y-1;
    $r2 = $n-$y;
    $c1 = $x-1;
    $c2 = $n-$x;
 
    // For each obstacle find the minimum distance. 
    // If obstacle is present in any direction, 
    // distance will be updated. 
 
    for ($i = 0; $i < $k; $i++)
    {
        if ( $x > $obstPosx[$i] && $y > $obstPosy[$i] &&
                $x-$obstPosx[$i] == $y-$obstPosy[$i] )
            $d11 = min($d11, $x-$obstPosx[$i]-1);
 
        if ( $obstPosx[$i] > $x && $obstPosy[$i] > $y &&
                $obstPosx[$i]-$x == $obstPosy[$i]-$y )
            $d12 = min( $d12, $obstPosx[$i]-$x-1);
 
        if ( $obstPosx[$i] > $x && $y > $obstPosy[$i] &&
                $obstPosx[$i]-$x == $y-$obstPosy[$i] )
            $d21 = min($d21, $obstPosx[$i]-$x-1);
 
        if ( $x > $obstPosx[$i] && $obstPosy[$i] > $y &&
                    $x-$obstPosx[$i] == $obstPosy[$i]-$y )
            $d22 = min($d22, $x-$obstPosx[$i]-1);
 
        if ( $x == $obstPosx[$i] && $obstPosy[$i] < $y )
            $r1 = min($r1, $y-$obstPosy[$i]-1);
 
        if ( $x == $obstPosx[$i] && $obstPosy[$i] > $y )
            $r2 = min($r2, $obstPosy[$i]-$y-1);
 
        if ( $y == $obstPosy[$i] && $obstPosx[$i] < $x )
            $c1 = min($c1, $x-$obstPosx[$i]-1);
 
        if ( $y == $obstPosy[$i] && $obstPosx[$i] > $x )
            $c2 = min($c2, $obstPosx[$i]-$x-1);
    }
 
    return $d11 + $d12 + $d21 + $d22 + $r1 + $r2 + $c1 + $c2;
}
 
// Driver code 
 
$n = 8; // Chessboard size 
$k = 1; // number of obstacles 
$Qposx = 4; // Queen x position 
$Qposy = 4; // Queen y position 
$obstPosx = array(3 ); // x position of obstacles 
$obstPosy = array(5 ); // y position of obstacles 
 
echo numberofPosition($n, $k, $Qposx, $Qposy,
                        $obstPosx, $obstPosy);
 
 
?>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder