need help translating a PHP for statement to python

Peter Otten __peter__ at web.de
Sat Jan 10 07:05:19 EST 2004


Paul Rubin wrote:

> Python is more verbose than PHP when it comes to stuff like that.

<verbosity.py>
marques = """for ($y = 0, $x = $cx-$cy-1; $y <= $cy; ++$y,++$x) {
    $prd = $q * $y_ar[$y] + $car;
    $prd -= ($car = intval($prd / 1E7)) * 1E7;
    if ($bar = (($x_ar[$x] -= $prd + $bar) < 0)) $x_ar[$x] += 1E7;
    }"""

rubin = """x = cx - cy - 1
for y in xrange(cy+1):
    prd = q * y_ar[y] + car
    car = int(prd / 1E7)
    prd -= car * 1E7
    x_ar[x] -= prd + bar
    bar = x_ar[x] < 0
    if bar:
        x_ar[x] += 1E7
    x += 1"""


if __name__ == "__main__":
    print "The ultimate verbosity check :-)"
    print "PHP", len(marques), "characters"
    print "Python", len(rubin), "characters"
</verbosity.py>

Output:

The ultimate verbosity check :-)
PHP 206 characters
Python 205 characters

Note that I didn't cheat and let

if bar: x_ar[x] += 1E7

spread over two lines, thus deliberately wasting another 8 characters.

Peter




More information about the Python-list mailing list