need help translating a PHP for statement to python

D

Davis Marques

hi;

I'm translating some PHP scripts to Python and have hit a roadblock
with a for statement. If someone could explain to me how one should
translate the multiple increment, evaluations, etc. in the first line
I would appreciate it deeply ...

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;
}

thanks,


Davis
 
P

Paul Rubin

I'm translating some PHP scripts to Python and have hit a roadblock
with a for statement. If someone could explain to me how one should
translate the multiple increment, evaluations, etc. in the first line
I would appreciate it deeply ...

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;
}

Python is more verbose than PHP when it comes to stuff like that.
You have to expand things:

x = cx - cy - 1
for y in xrange(cy+1):
prd = q * y_ar[y] + car;
car = intval(prd / 1E7)
prd -= car * 1E7;
x_ar[x] -= prd + bar
bar = (x_ar[x] < 0) # is this REALLY what you wanted?
if bar:
x_ar[x] += 1E7;
x += 1

You can more closely approximate the two parallel loops with the
iterzip function in the new itertools package, but that only works
with the latest Python versions.

I suspect that your if statement was actually supposed to say

if (($bar = ($x_ar[$x] -= $prd + $bar)) < 0) $x_ar[$x] += 1E7;

but I don't know what you're really trying to do.
 
E

Eli Pollak

Haha ... you must be originally a perl coder. Who else writes attricious
code like this
if ($bar = (($x_ar[$x] -= $prd + $bar) < 0))
If I was hiring you, I'd fire you on the spot.

Secondly, here is what I came up with. Not sure what '1E7' is though.
And change 'intval' to 'int' if you want to cast something to an int in
python

x,y = cx-cy-1,0
while y <= cy:
prd = q * y_ar[y] + car - ((car = int(prd / 1E7)) * 1E7);
if (bar = ((x_ar[x] -= prd + bar) < 0)) x_ar[x] += 1E7;
x,y = x+1,y+1

Eli
 
P

Peter Otten

Paul said:
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
 
D

Dave Benjamin

Haha ... you must be originally a perl coder. Who else writes attricious
code like this
if ($bar = (($x_ar[$x] -= $prd + $bar) < 0))

I know, I know! C programmers! Right?
 
D

Dang Griffith

Haha ... you must be originally a perl coder. Who else writes attricious
code like this
if ($bar = (($x_ar[$x] -= $prd + $bar) < 0))

Any C programmer worth his salt. I miss those days. Sort of.
--dang
 
P

Paul Rubin

Dang Griffith said:
Haha ... you must be originally a perl coder. Who else writes attricious
code like this
if ($bar = (($x_ar[$x] -= $prd + $bar) < 0))

Any C programmer worth his salt. I miss those days. Sort of.


But that line almost certainly has the parentheses in the wrong place.
Look carefully.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top