473,418 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

need help translating a PHP for statement to python

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
Jul 18 '05 #1
6 1527
dm*@machinic.net (Davis Marques) writes:
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.
Jul 18 '05 #2
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
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

Jul 18 '05 #3
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

Jul 18 '05 #4
In article <ma**************************************@python.o rg>, Eli Pollak wrote:
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?

--
..:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
: d r i n k i n g l i f e o u t o f t h e c o n t a i n e r :
Jul 18 '05 #5
On Sat, 10 Jan 2004 20:22:18 +1100, "Eli Pollak" <no**@none.com>
wrote:
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
Jul 18 '05 #6
Dang Griffith <no*****@noemail4u.com> writes:
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.
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Jeff Wagner | last post by:
I am in the process of learning Python (obsessively so). I've been through a few tutorials and read a Python book that was lent to me. I am now trying to put what I've learned to use by rewriting...
2
by: Henrik S. Hansen | last post by:
How do you best go about translating characters like '\\n' to '\n'? This is for a configuration file parser, where the "backslash convention" is supported. The naive approach --...
45
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
0
by: Dylan Phillips | last post by:
I'm interested in how other participants in this new group are implementing SQL Full-Text Search on their Web Sites. How are you translating the user search string: "DirectX managed code" into...
6
by: Jumping Matt Flash | last post by:
The code i'm writing is using VB .NET and is for a web service returning a dataset, which is in turn to be used by an ASP .NET application displaying a datagrid. I'm currently populating a...
6
by: Patrick Sullivan | last post by:
I want to use this algorithm but can't figure it out, I never used BASIC. I tried translating it but got lost in the gosubs and "for i - 1 to ... nexts". I have figured out that FNU(X) is degree...
2
by: vj | last post by:
I have a perl script which connect to network stream using sockets. The scripts first logins in to the server and then parses the data comming from the socket. Statement 1: my $today =...
1
by: trihaitran | last post by:
Hi I am new to Ruby from Python and am still learning some stuff. I have a Ruby program that uses the mechanize library to connect to a Web site and authenticate with a username and password. The...
15
by: hofer | last post by:
Hi, Let's take following perl code snippet: %myhash=( one =1 , two =2 , three =3 ); ($v1,$v2,$v3) = @myhash{qw(one two two)}; # <-- line of interest print "$v1\n$v2\n$v2\n"; How...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.