473,725 Members | 2,197 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1555
dm*@machinic.ne t (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************ *************** ***********@pyt hon.org>, 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.co m>
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*****@noemai l4u.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
3098
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 that Numerology program I wrote years ago in VB. There are times I am totally stuck (for instance, I just had an idea to put the numerical values of the alphabet and months of the year in a dictionary located in a function. Then, I can import the...
2
1445
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 -- re.sub('\\\\(.)', '\\\1', s) -- doesn't work, of course. The best I've come up with so far is a special case for every character to be translated. There must be an easier way? -- Henrik S. Hansen http://freecode.dk/~hsh/
45
3043
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
1120
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 the contains statement syntax ' "DirectX" AND "managed" AND "code" '? I started writing a query parsers as an intermediary to handle control characters like , but frankly it's becoming a rather pain in the a$$. If anyone has any suggestions as...
6
2532
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 datagrid using a "select top 1 column 1, column2, column3 from tblTable" statement. As there will only ever be one row returned I want to be able to to switch the column names to be row1 and the column values to be row 2 i.e. select house, street,...
6
1358
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 to radians, and FNY is a modulo, but those are not what get me confused, it's just this section here. Anyone in here with "old school BASIC" experience who can unravel this for fun? :) 1740 DEF FN Y(X) = ATN(SQR(1 - X * X) / X):Y = 0:MO = 360...
2
1934
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 = sprintf("%4s%02s%02s", ->+1900, ->+1, ->) ; Statement 2:
1
3197
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 issue I have is that I need to get this code translated into Python. The Python mechanize library is not very well documented and I was wondering if anyone here had any experience using this library in Ruby or Python. Basically the only tricky...
15
5882
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 do I translate the second line in a similiar compact way to python?
0
8888
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8097
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.