Need help in understanding a python code

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Sun Nov 16 08:50:36 CET 2008


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



More information about the Python-list mailing list