473,804 Members | 3,855 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help in understanding a python code

Hi,

I am trying to understand the following line:
# a is an integer array

max([(sum(a[j:i]), (j,i))

Can you please tell me what that means,
I think sum(a[j:i] means find the some from a[j] to a[i]
But what is the meaning of the part (j,i)?

Nov 16 '08 #1
15 1675
On Sat, Nov 15, 2008 at 8:41 PM, si************* **@gmail.com
<si************ ***@gmail.comwr ote:
Hi,

I am trying to understand the following line:
# a is an integer array

max([(sum(a[j:i]), (j,i))
This code isn't valid. You have a [ with no closing ].

Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
>
Can you please tell me what that means,
I think sum(a[j:i] means find the some from a[j] to a[i]
But what is the meaning of the part (j,i)?

--
http://mail.python.org/mailman/listinfo/python-list
Nov 16 '08 #2
On Nov 16, 3:41*pm, "silverburgh.me ...@gmail.com"
<silverburgh.me ...@gmail.comwr ote:
Hi,

I am trying to understand the following line:
# a is an integer array

max([(sum(a[j:i]), (j,i))

Can you please tell me what that means,
I think sum(a[j:i] means find the some from a[j] to a[i]
But what is the meaning of the part (j,i)?
0. "integer array" is a very loose term in Python. Fortunately the
answer to your question is not affected by that.
1. Sorry, the max... line is not syntactically correct; there are two
[s and only one ]; there are 4 (s and only 3 )s. Try copying the line
and pasting, not re-typing.
2. I'm not going to try to guess how to fix the bracket mismatches.
3. Note that you have left off a ) from your question about "sum" ...
it probably should be sum([j:i]).
4. That is the sum (not "some"!!) of a[j] to a[i-1] both inclusive.
It's a standard idiom in Python for the end of a range to be expressed
as the first unused element.
5. Even after fixing the bracket mismatches, it looks like you will
have an expression whose value is thrown away. Perhaps you might like
to give us a few lines of context before and after the line of
interest.
Nov 16 '08 #3
This is the full source code:
def A(w, v, i,j):
if i == 0 or j == 0: return 0
if w[i-1] j: return A(w, v, i-1, j)
if w[i-1] <= j: return max(A(w,v, i-1, j), v[i-1] + A(w,v, i-1, j - w[i-1]))

I am reading this blog

http://20bits.com/articles/introduct...c-programming/
On Sat, Nov 15, 2008 at 10:54 PM, Chris Rebert <cl*@rebertia.c omwrote:
On Sat, Nov 15, 2008 at 8:41 PM, si************* **@gmail.com
<si************ ***@gmail.comwr ote:
>Hi,

I am trying to understand the following line:
# a is an integer array

max([(sum(a[j:i]), (j,i))

This code isn't valid. You have a [ with no closing ].

Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
>>
Can you please tell me what that means,
I think sum(a[j:i] means find the some from a[j] to a[i]
But what is the meaning of the part (j,i)?

--
http://mail.python.org/mailman/listinfo/python-list
Nov 16 '08 #4
See below.

On Nov 15, 11:15*pm, "Meryl Silverburgh" <silverburgh.me ...@gmail.com>
wrote:
This is the full source code:
def A(w, v, i,j):
* * if i == 0 or j == 0: return 0
* * if w[i-1] j: *return A(w, v, i-1, j)
* * if w[i-1] <= j: return max(A(w,v, i-1, j), v[i-1] + A(w,v, i-1,j - w[i-1]))

I am reading this blog

http://20bits.com/articles/introduct...c-programming/

On Sat, Nov 15, 2008 at 10:54 PM, Chris Rebert <c...@rebertia. comwrote:
On Sat, Nov 15, 2008 at 8:41 PM, silverburgh.me. ..@gmail.com
<silverburgh.me ...@gmail.comwr ote:
Hi,
I am trying to understand the following line:
# a is an integer array
max([(sum(a[j:i]), (j,i))
This code isn't valid. You have a [ with no closing ].
Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
Can you please tell me what that means,
I think sum(a[j:i] means find the some from a[j] to a[i]
But what is the meaning of the part (j,i)?
--
http://mail.python.org/mailman/listinfo/python-list

if w[i-1] <= j: return max(A(w,v, i-1, j), v[i-1] + A(w,v, i-1, j -w[i-1]))
This means:

Calculate 'A(w,v, i-1, j)', calculate 'v[i-1] + A(w,v, i-1, j - w
[i-1])', and return whichever is larger.
Nov 16 '08 #5
silverburgh:
max([(sum(a[j:i]), (j,i))
Other people have already answered you so I'll add only a small note:
today the max() function has a key optional attribute, so that code
can also be written as:

max(((j, i) for ...), key=lambda (j, i): sum(a[j : i]))

I think you have copied that part from code that runs in O(n^2);
remember that you can find the max subarray with a well known O(n)
algorithm too.

Bye,
bearophile
Nov 16 '08 #6
On Nov 16, 4:15*pm, "Meryl Silverburgh" <silverburgh.me ...@gmail.com>
wrote:
This is the full source code:
def A(w, v, i,j):
* * if i == 0 or j == 0: return 0
* * if w[i-1] j: *return A(w, v, i-1, j)
* * if w[i-1] <= j: return max(A(w,v, i-1, j), v[i-1] + A(w,v, i-1,j - w[i-1]))
Huh??? There is only a very slight resemblance to the code that you
posted previously ... both contain 'max, 'i', and 'j'
I am reading this blog

http://20bits.com/articles/introduct...c-programming/
I suggest that you don't bother reading a blog written by somebody who
(presumably consciously) keyed in that "if w[i-1] <= j: " above.

Oh, very interesting, it contains:
def msum(a):
return max([(sum(a[j:i]), (j,i)) for i in range(1,len(a)+ 1) for j
in range(i)])

Would you care to tell us which part of which function you are now
trying to understand?
Nov 16 '08 #7
On Sun, 16 Nov 2008 01:50:16 -0800, John Machin wrote:
def A(w, v, i,j):
Â* Â* if i == 0 or j == 0: return 0
Â* Â* if w[i-1] j: Â*return A(w, v, i-1, j)
Â* Â* if w[i-1] <= j: return max(A(w,v, i-1, j), v[i-1] +
A(w,v, i-1, j - w[i-1]))
>I am reading this blog

http://20bits.com/articles/introduct...c-programming/

I suggest that you don't bother reading a blog written by somebody who
(presumably consciously) keyed in that "if w[i-1] <= j: " above.
That is a translation of standard terminology for a hybrid function.
Mathematics doesn't have an "else", so you write hybrid functions by
enumerating each branch as an if.

While it's not especially good Python technique, it's a perfectly
idiomatic mathematical expression, and shouldn't be the basis for
dismissing an entire blog.
--
Steven
Nov 16 '08 #8
On Nov 16, 9:31*pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com .auwrote:
On Sun, 16 Nov 2008 01:50:16 -0800, John Machin wrote:
def A(w, v, i,j):
* * if i == 0 or j == 0: return 0
* * if w[i-1] j: *return A(w, v, i-1, j)
* * if w[i-1] <= j: return max(A(w,v, i-1, j), v[i-1] +
* * * A(w,v, i-1, j - w[i-1]))
I am reading this blog
>http://20bits.com/articles/introduct...c-programming/
I suggest that you don't bother reading a blog written by somebody who
(presumably consciously) keyed in that "if w[i-1] <= j: " above.

That is a translation of standard terminology for a hybrid function.
Mathematics doesn't have an "else", so you write hybrid functions by
enumerating each branch as an if.
An else is not required.
if w[i-1] j:
return A(w, v, i-1, j)
return max(A(w,v, i-1, j), v[i-1] + A(w,v, i-1, j - w[i-1]))
While it's not especially good Python technique, it's a perfectly
idiomatic mathematical expression, and shouldn't be the basis for
dismissing an entire blog.
He's meant to be writing Python code, not mathematical expressions.
Nov 16 '08 #9
On Sun, 16 Nov 2008 02:41:03 -0800, John Machin wrote:
On Nov 16, 9:31Â*pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com .auwrote:
>On Sun, 16 Nov 2008 01:50:16 -0800, John Machin wrote:
def A(w, v, i,j):
Â* Â* if i == 0 or j == 0: return 0
Â* Â* if w[i-1] j: Â*return A(w, v, i-1, j) if w[i-1] <= j: return
Â* Â* max(A(w,v, i-1, j), v[i-1] +
Â* Â* Â* A(w,v, i-1, j - w[i-1]))
I am reading this blog
>>http://20bits.com/articles/introduct...c-programming/
I suggest that you don't bother reading a blog written by somebody
who (presumably consciously) keyed in that "if w[i-1] <= j: " above.

That is a translation of standard terminology for a hybrid function.
Mathematics doesn't have an "else", so you write hybrid functions by
enumerating each branch as an if.

An else is not required.
if w[i-1] j:
return A(w, v, i-1, j)
return max(A(w,v, i-1, j), v[i-1] + A(w,v, i-1, j - w[i-1]))
Which is also not valid terminology for hybrid functions.

>While it's not especially good Python technique, it's a perfectly
idiomatic mathematical expression, and shouldn't be the basis for
dismissing an entire blog.

He's meant to be writing Python code, not mathematical expressions.
And he's written Python code. Perfectly valid Python code. Just because
it is not what you consider to be idiomatic Python code isn't a good
reason to dismiss his entire blog.

What you've done is rather like me saying that because you failed to use
a colon after "required", and therefore haven't written what *I* consider
good English style, not only is your specific post best avoided, but
*all* your posts should be avoided. I trust you understand the logical
fallacy I would be making, which you have already made.

http://en.wikipedia.org/wiki/Style_o...stance_fallacy

--
Steven
and now begins the arguments as to whether it is a fallacy, and if so, if
it is the fallacy I have said it is...

Nov 16 '08 #10

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

Similar topics

3
11049
by: Graham Nicholls | last post by:
Hi, I'm trying to size a jpeg file. The file size is held in a short (2 byte integer) at a certain offset. Once I've found these two bytes (they're in MSB,LSB order), I need to convert them to an integer - now I know that in C I'd just cast a pointer to the offset to a short, and that python doesn't cast, so how can I extract the value from a stream of bytes. I've looked at python.org/Doc/current (I'm using 2.3b1), but can't find...
9
2146
by: Roy Smith | last post by:
I'm working on a prototype of a new application in Python. At some point, if this ever turns into a product, the powers that be will almost certainly demand that it be done in Perl. My job will be to convince them otherwise. The basic design of the application is object oriented. I've never used Perl's OO features, so I'm not in a good position to make a comparison of the two languages from an OO point of view. Can somebody who's...
10
3110
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...
6
11493
by: Fuzzyman | last post by:
Ok.... so I might be a windoze user trying to program CGIs for a Linux server.... but Python doesn't seem to go out of it's way to make understanding file attributes difficult. The python manual is appalling in this are a :-( Anyway - I think I've finally worked out that the correct way to get (rather than set) the mode of a file is : from stat import * S_IMODE(os.stat(filepath))
2
1347
by: Dhruva Hein | last post by:
Hi. I am trying to understand a section of code written for Plone and I am having problems understanding the Python syntax in a few of the following lines. I'd be grateful if someone could help me understand what's happening in the lines I've marked. I can't see how the dictionary is built or how the lists make sense in python. Thanks in advance.
4
1565
by: John Salerno | last post by:
Here's some code from Python in a Nutshell. The comments are lines from a previous example that the calls to super replace in the new example: class A(object): def met(self): print 'A.met' class B(A): def met(self): print 'B.met'
0
916
by: Thomas Ploch | last post by:
Hello folks, I am thinking about reading and understanding the Source Code of Python, but where would it be best to start? Possibly someone can give me a little hint. I am getting into socketmodule.c a little bit at the moment, but thats not what I want. Greetz, Thomas -- Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen! Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer
3
1596
by: Greg Corradini | last post by:
Hello, I'm trying to perform a simple insert statement into a table called Parcel_Test (see code below). Yet, I get an error message that I've never seen before (see traceback below). I've tried to put a semicolon at the end of the sql statement, but with no luck. Any ideas from more experienced mx.ODBC users? CODE TRACEBACK
6
1412
by: Steven W. Orr | last post by:
Given the following code: (I hope it's as simple as possible) :-) #! /usr/bin/python import new class BASE: def __init__( self ): print 'Hello from BASE init' def m1( self ): print 'M1 Base: Self = ', self def m1replace( self ):
0
9577
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,...
0
10569
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10325
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10315
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
10075
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
6847
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5519
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...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
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

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.