From: Andre Alexander Bell on
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
>>> numpy.sum(signs*squares)
536926141

Cheers


Andre

From: Jean-Michel Pichavant on
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(sign*i*i for sign, i in izip(cycle([1]*3+[-1]*2), range(1,
>>>>>> 2011)))
>>> 536926141
>>
>> don't understand it bit i like this a lot!
> ^^^
>
> *but*
Works for women as well, we don't understand them, yet we love them ;)

JM
From: Stefan Behnel on
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 for sign, i in izip(cycle([1]*3+[-1]*2), range(1, 2011)))
>> 536926141
>
> don't understand it bit i like this a lot!

Didn't you want to get it "pythonized"? If it's not understandable, it
can't be pythonic.

Stefan

From: superpollo on
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 cycle, izip
>>>>>> sum(sign*i*i for sign, i in izip(cycle([1]*3+[-1]*2), range(1,
>>>>>> 2011)))
>>> 536926141
>>
>> don't understand it bit i like this a lot!
>
> Didn't you want to get it "pythonized"? If it's not understandable, it
> can't be pythonic.

maybe i must study itertools then ;-)

thanks

bye
From: Peter Otten on
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)
>>>
>>>>>> from itertools import cycle, izip
>>>>>> sum(sign*i*i for sign, i in izip(cycle([1]*3+[-1]*2), range(1,
>>>>>> 2011)))
>>> 536926141
>>
>> don't understand it bit i like this a lot!
>
> Didn't you want to get it "pythonized"? If it's not understandable, it
> can't be pythonic.

I'm glad I didn't have to say that mayself ;)

OP: You can work it out step by step:

First build a list of signs:

>>> [1]*3+[-1]*2
[1, 1, 1, -1, -1]

Then repeat them infinitely:

>>> c = cycle("xy")
>>> c.next()
'x'
>>> c.next()
'y'
>>> c.next()
'x'
>>> c.next()
'y'
>>> c.next()
'x'

Combine with the bases using izip:

>>> signs = cycle([1]*3+[-1]*2)
>>> [sign*i for sign, i in izip(signs, range(10))]
[0, 1, 2, -3, -4, 5, 6, 7, -8, -9]

Finally calculate the sum:
>>> signs = cycle([1]*3+[-1]*2)
>>> sum(sign*i for sign, i in izip(signs, range(10)))
-3

Peter