Re: pythonize this!

2010-06-19 Thread Mark Lawrence
On 19/06/2010 11:36, Stefan Behnel wrote: Mark Lawrence, 18.06.2010 17:53: ... *AND* (looking at your email address) Germany loosing in the world cup. :( Yep, we always do that once at the early stages of a world cup. Pretty good camouflage, still works most of the time. Stefan Yes, but tr

Re: pythonize this!

2010-06-19 Thread Stefan Behnel
Mark Lawrence, 18.06.2010 17:53: ... *AND* (looking at your email address) Germany loosing in the world cup. :( Yep, we always do that once at the early stages of a world cup. Pretty good camouflage, still works most of the time. Stefan -- http://mail.python.org/mailman/listinfo/python-list

Re: pythonize this!

2010-06-19 Thread Andre Alexander Bell
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 06/18/2010 05:53 PM, Mark Lawrence wrote: > Andre, looks like a really bad day for you then, *TWO* nights out with > me *AND* (looking at your email address) Germany loosing in the world > cup. :( There are days one looses and there are days the ot

Re: pythonize this!

2010-06-18 Thread Mark Lawrence
On 18/06/2010 16:26, Andre Alexander Bell wrote: On 06/18/2010 03:32 PM, Mark Lawrence wrote: The good news is that this is easily the fastest piece of code that I've seen yet. The bad news is that first prize in the speed competition is a night out with me. :) Well, that actually means that

Re: pythonize this!

2010-06-18 Thread Andre Alexander Bell
On 06/18/2010 03:32 PM, Mark Lawrence wrote: > The good news is that this is easily the fastest piece of code that I've > seen yet. The bad news is that first prize in the speed competition is > a night out with me. :) Well, that actually means that Stefan Behnel will run my solution through cyth

Re: pythonize this!

2010-06-18 Thread Mark Lawrence
On 18/06/2010 16:00, Steven D'Aprano wrote: On Fri, 18 Jun 2010 14:32:30 +0100, Mark Lawrence wrote: The good news is that this is easily the fastest piece of code that I've seen yet. The bad news is that first prize in the speed competition is a night out with me. I suppose second prize is

Re: pythonize this!

2010-06-18 Thread Steven D'Aprano
On Fri, 18 Jun 2010 14:32:30 +0100, Mark Lawrence wrote: > The good news is that this is easily the fastest piece of code that I've > seen yet. The bad news is that first prize in the speed competition is > a night out with me. I suppose second prize is two nights out with you? -- Steven --

Re: pythonize this!

2010-06-18 Thread Mark Lawrence
On 18/06/2010 10:23, Andre Alexander Bell wrote: On 06/16/2010 12:47 PM, Lie Ryan wrote: Probably bending the rules a little bit: sum(x**2 - 8*x - 20 for x in range(1, 2010, 5)) 536926141 Bending them even further, the sum of the squares from 1 to N is given by (1) N*(N+1)*(2*N+1)/6. The

Re: pythonize this!

2010-06-18 Thread Stefan Behnel
Peter Otten, 18.06.2010 12:14: Stefan Behnel wrote: Andre Alexander Bell, 18.06.2010 11:23: On 06/16/2010 12:47 PM, Lie Ryan wrote: Probably bending the rules a little bit: sum(x**2 - 8*x - 20 for x in range(1, 2010, 5)) 536926141 Bending them even further, the sum of the squares from 1

Re: pythonize this!

2010-06-18 Thread Peter Otten
Stefan Behnel wrote: > Andre Alexander Bell, 18.06.2010 11:23: >> On 06/16/2010 12:47 PM, Lie Ryan wrote: >>> Probably bending the rules a little bit: >>> >> sum(x**2 - 8*x - 20 for x in range(1, 2010, 5)) >>> 536926141 >> >> Bending them even further, the sum of the squares from 1 to N is giv

Re: pythonize this!

2010-06-18 Thread Stefan Behnel
Andre Alexander Bell, 18.06.2010 11:23: On 06/16/2010 12:47 PM, Lie Ryan wrote: Probably bending the rules a little bit: sum(x**2 - 8*x - 20 for x in range(1, 2010, 5)) 536926141 Bending them even further, the sum of the squares from 1 to N is given by (1) N*(N+1)*(2*N+1)/6. The given pro

Re: pythonize this!

2010-06-18 Thread Andre Alexander Bell
On 06/16/2010 12:47 PM, Lie Ryan wrote: > Probably bending the rules a little bit: > sum(x**2 - 8*x - 20 for x in range(1, 2010, 5)) > 536926141 Bending them even further, the sum of the squares from 1 to N is given by (1) N*(N+1)*(2*N+1)/6. The given problem can be divided into five sums

Re: pythonize this!

2010-06-16 Thread Boris Borcic
Ignacio Mondino wrote: On Tue, Jun 15, 2010 at 8:49 AM, superpollo wrote: goal (from e.c.m.): evaluate 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each three consecutive + must be followed by two - (^ meaning ** in this context) my solution: s = 0 for i in range(1, 2011): ...

Re: pythonize this!

2010-06-16 Thread Richard Brodie
"Lie Ryan" wrote in message news:4c18a...@dnews.tpgi.com.au... > Probably bending the rules a little bit: > sum(x**2 - 8*x - 20 for x in range(1, 2010, 5)) > 536926141 Or, letting Python do the algera for you: >>> from sympy import var, sum >>> dummy = var('j k') >>> k = (5 * j) + 1 >>>

Re: pythonize this!

2010-06-16 Thread Stefan Behnel
Jussi Piitulainen, 16.06.2010 13:10: Lie Ryan writes: On 06/15/10 21:49, superpollo wrote: goal (from e.c.m.): evaluate 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each three consecutive + must be followed by two - (^ meaning ** in this context) [...] Probably bending the rule

Re: pythonize this!

2010-06-16 Thread Jussi Piitulainen
Lie Ryan writes: > On 06/15/10 21:49, superpollo wrote: > > goal (from e.c.m.): evaluate > > 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each > > three consecutive + must be followed by two - (^ meaning ** in > > this context) [...] > Probably bending the rules a little bit: > > >>

Re: pythonize this!

2010-06-16 Thread Lie Ryan
On 06/15/10 21:49, superpollo wrote: > goal (from e.c.m.): evaluate > 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each three > consecutive + must be followed by two - (^ meaning ** in this context) > > my solution: > s = 0 for i in range(1, 2011): > s += i**2 > .

Re: pythonize this!

2010-06-16 Thread Alessandro [AkiRoss] Re
On Jun 15, 2:37 pm, Peter Otten <__pete...@web.de> wrote: > >>> from itertools import cycle, izip > >>> sum(sign*i*i for sign, i in izip(cycle([1]*3+[-1]*2), range(1, 2011))) Wow!! :D I didn't knew cycle, great! With that i can reduce my solution (which isn't still elegant as your) to: print redu

Re: pythonize this!

2010-06-16 Thread Alessandro [AkiRoss] Re
On Jun 15, 1:49 pm, superpollo wrote: > goal (from e.c.m.): evaluate > 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each three > consecutive + must be followed by two - (^ meaning ** in this context) My functional approach :) from operator import add from functools import reduce

Re: pythonize this!

2010-06-15 Thread Dan Stromberg
On Tue, Jun 15, 2010 at 9:26 AM, Ian Kelly wrote: > On Tue, Jun 15, 2010 at 6:21 AM, Alain Ketterlin > wrote: > > You compute i**2 too many times (7/5 times more than necessary) and > > twice too many modulos. I suggest: > > > > c = { 0:1, 1:1, 2:1, 3:-1, 4:-1 } > > #or, why not: c = lambda i :

Re: pythonize this!

2010-06-15 Thread Ignacio Mondino
On Tue, Jun 15, 2010 at 8:49 AM, superpollo wrote: > goal (from e.c.m.): evaluate > 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each three > consecutive + must be followed by two - (^ meaning ** in this context) > > my solution: > s = 0 for i in range(1, 2011): > ...     s

Re: pythonize this!

2010-06-15 Thread superpollo
Paul Rubin ha scritto: superpollo writes: goal (from e.c.m.): evaluate 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each three consecutive + must be followed by two - (^ meaning ** in this context) print sum([-1,1,1,1,-1][i%5]*i**2 for i in xrange(1,2011)) beautiful. thx -- h

Re: pythonize this!

2010-06-15 Thread Paul Rubin
superpollo writes: > goal (from e.c.m.): evaluate > 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each three > consecutive + must be followed by two - (^ meaning ** in this context) print sum([-1,1,1,1,-1][i%5]*i**2 for i in xrange(1,2011)) -- http://mail.python.org/mailman/listinfo

Re: pythonize this!

2010-06-15 Thread Ian Kelly
On Tue, Jun 15, 2010 at 6:21 AM, Alain Ketterlin wrote: > You compute i**2 too many times (7/5 times more than necessary) and > twice too many modulos. I suggest: > > c = { 0:1, 1:1, 2:1, 3:-1, 4:-1 } > #or, why not: c = lambda i : +1 if (i%5) < 3 else -1 > > s = 0 > for i in range(1,2011): >    s

Re: pythonize this!

2010-06-15 Thread Peter Otten
Stefan Behnel wrote: > superpollo, 15.06.2010 14:55: >> Peter Otten ha scritto: >>> superpollo wrote: >>> goal (from e.c.m.): evaluate 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each three consecutive + must be followed by two - (^ meaning ** in this context) >>> >>>

Re: pythonize this!

2010-06-15 Thread superpollo
Stefan Behnel ha scritto: superpollo, 15.06.2010 14:55: Peter Otten ha scritto: superpollo wrote: goal (from e.c.m.): evaluate 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each three consecutive + must be followed by two - (^ meaning ** in this context) from itertools import

Re: pythonize this!

2010-06-15 Thread Stefan Behnel
superpollo, 15.06.2010 14:55: Peter Otten ha scritto: superpollo wrote: goal (from e.c.m.): evaluate 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each three consecutive + must be followed by two - (^ meaning ** in this context) from itertools import cycle, izip sum(sign*i*i fo

Re: pythonize this!

2010-06-15 Thread Shashwat Anand
>>> sum(i*i*(-1)**((i % 5) / 4 + (i + 4) % 5 / 4) for i in range(1,2011)) 536926141 On Tue, Jun 15, 2010 at 6:25 PM, superpollo wrote: > superpollo ha scritto: > > Peter Otten ha scritto: >> >>> superpollo wrote: >>> >>> goal (from e.c.m.): evaluate 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-1

Re: pythonize this!

2010-06-15 Thread Jean-Michel Pichavant
superpollo wrote: superpollo ha scritto: Peter Otten ha scritto: superpollo wrote: goal (from e.c.m.): evaluate 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each three consecutive + must be followed by two - (^ meaning ** in this context) from itertools import cycle, izip sum

Re: pythonize this!

2010-06-15 Thread Andre Alexander Bell
On 06/15/2010 01:49 PM, superpollo wrote: > my solution: > > [...] > >>> print s > 536926141 Or, if you would like to use numpy: >>> import numpy >>> squares = numpy.arange(1, 2011, dtype=numpy.int)**2 >>> signs = numpy.ones(len(squares), dtype=numpy.int) >>> signs[3::5] = -1 >>> signs[4::5] = -1

Re: pythonize this!

2010-06-15 Thread Xavier Ho
On 15 June 2010 22:55, superpollo wrote: > Peter Otten ha scritto: > > superpollo wrote: >> >> goal (from e.c.m.): evaluate >>> 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each three >>> consecutive + must be followed by two - (^ meaning ** in this context) >>> >> >> from iterto

Re: pythonize this!

2010-06-15 Thread Stefan Behnel
Stefan Behnel, 15.06.2010 14:23: superpollo, 15.06.2010 13:49: goal (from e.c.m.): evaluate 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each three consecutive + must be followed by two - (^ meaning ** in this context) my solution: >>> s = 0 >>> for i in range(1, 2011): ... s +=

Re: pythonize this!

2010-06-15 Thread superpollo
superpollo ha scritto: Peter Otten ha scritto: superpollo wrote: goal (from e.c.m.): evaluate 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each three consecutive + must be followed by two - (^ meaning ** in this context) from itertools import cycle, izip sum(sign*i*i for sign,

Re: pythonize this!

2010-06-15 Thread superpollo
Peter Otten ha scritto: superpollo wrote: goal (from e.c.m.): evaluate 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each three consecutive + must be followed by two - (^ meaning ** in this context) from itertools import cycle, izip sum(sign*i*i for sign, i in izip(cycle([1]*3+[

Re: pythonize this!

2010-06-15 Thread Peter Otten
superpollo wrote: > goal (from e.c.m.): evaluate > 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each three > consecutive + must be followed by two - (^ meaning ** in this context) >>> from itertools import cycle, izip >>> sum(sign*i*i for sign, i in izip(cycle([1]*3+[-1]*2), range(1

Re: pythonize this!

2010-06-15 Thread Jussi Piitulainen
superpollo writes: > goal (from e.c.m.): evaluate > 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each three > consecutive + must be followed by two - (^ meaning ** in this context) > > my solution: > > >>> s = 0 > >>> for i in range(1, 2011): > ... s += i**2 > ... if not

Re: pythonize this!

2010-06-15 Thread superpollo
Ulrich Eckhardt ha scritto: superpollo wrote: ... s += i**2 ... if not (i+1)%5: ... s -= 2*i**2 ... if not i%5: ... s -= 2*i**2 if not (i % 5) in [1, 2]: s += i**2 else: s -= i**2 Untested code. does not work: >>> s = 0 >>> for i in range(1, 2011): ...

Re: pythonize this!

2010-06-15 Thread Alain Ketterlin
superpollo writes: > goal (from e.c.m.): evaluate > 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each three > consecutive + must be followed by two - (^ meaning ** in this context) > > my solution: > s = 0 for i in range(1, 2011): > ... s += i**2 > ... if not (i+1)

Re: pythonize this!

2010-06-15 Thread Stefan Behnel
superpollo, 15.06.2010 13:49: goal (from e.c.m.): evaluate 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each three consecutive + must be followed by two - (^ meaning ** in this context) my solution: >>> s = 0 >>> for i in range(1, 2011): ... s += i**2 ... if not (i+1)%5: ... s -

Re: pythonize this!

2010-06-15 Thread Ulrich Eckhardt
superpollo wrote: > ... s += i**2 > ... if not (i+1)%5: > ... s -= 2*i**2 > ... if not i%5: > ... s -= 2*i**2 if not (i % 5) in [1, 2]: s += i**2 else: s -= i**2 Untested code. Uli -- Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR

Re: pythonize this!

2010-06-15 Thread Xavier Ho
On 15 June 2010 21:49, superpollo wrote: > goal (from e.c.m.): evaluate > 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each three > consecutive + must be followed by two - (^ meaning ** in this context) > Obligatory one-liner: >>> sum((1, 1, 1, -1, -1)[(x-1) % 5] * x**2 for x in x

pythonize this!

2010-06-15 Thread superpollo
goal (from e.c.m.): evaluate 1^2+2^2+3^2-4^2-5^2+6^2+7^2+8^2-9^2-10^2+...-2010^2, where each three consecutive + must be followed by two - (^ meaning ** in this context) my solution: >>> s = 0 >>> for i in range(1, 2011): ... s += i**2 ... if not (i+1)%5: ... s -= 2*i**2 ...

Re: Can anyone Pythonize this lexical algorithm?

2008-10-09 Thread Terry Reedy
ShashiGowda wrote: I am writing a package manager and stuck unable to write the version sorting function the algorithm is here http://www.linux.gr/cgi-bin/man/man2html?deb-version+5 and all other info is also in it please tell me how to do lexical comparision in python it'll be cool if you just

Re: Can anyone Pythonize this lexical algorithm?

2008-10-09 Thread Paul McGuire
On Oct 9, 7:29 am, ShashiGowda <[EMAIL PROTECTED]> wrote: > I am writing a package manager and stuck unable to write the version > sorting function the algorithm is > herehttp://www.linux.gr/cgi-bin/man/man2html?deb-version+5 > and all other info is also in it please tell me how to do lexical > co

Can anyone Pythonize this lexical algorithm?

2008-10-09 Thread ShashiGowda
I am writing a package manager and stuck unable to write the version sorting function the algorithm is here http://www.linux.gr/cgi-bin/man/man2html?deb-version+5 and all other info is also in it please tell me how to do lexical comparision in python it'll be cool if you just write the code! -- ht