Terry Reedy wrote:
> On 1/5/2015 6:33 AM, flebber wrote:
>>
>>> You could do what mathematicians do when they deal with alternating
>>> signs: they raise -1 to the power of the index to get an appropriate
>>> multiplier.
>>>
>>> >>> [ n * (-1) ** n for n in range(10) ]
>>> [0, -1, 2, -3, 4
On 1/5/2015 6:33 AM, flebber wrote:
You could do what mathematicians do when they deal with alternating
signs: they raise -1 to the power of the index to get an appropriate
multiplier.
>>> [ n * (-1) ** n for n in range(10) ]
[0, -1, 2, -3, 4, -5, 6, -7, 8, -9]
Mathematicians operati
> In py2, map produces a list already. In any case, above is syntax error
> without else clause.
>
> map(lambda x: x * -1 if x%2 else x, series)
>
> If you do not have a function already, a list comp is better.
>
> [(-1*k if k%2 else k) for k in range(2, N)]
>
> Change [] to () and you have
> You could do what mathematicians do when they deal with alternating
> signs: they raise -1 to the power of the index to get an appropriate
> multiplier.
>
>>>> [ n * (-1) ** n for n in range(10) ]
>[0, -1, 2, -3, 4, -5, 6, -7, 8, -9]
>>>>
>
> Or you could do here what you attempt
Thank you for those solutions so varied. Am going to have a party with them
to see what works best for me.
Strange is there a way too use lousy comprehension without returning just
the matching odd values?
Sayth
On Mon, 5 Jan 2015 5:06 AM Dan Stromberg wrote:
> I'm partial to:
>
> #!/usr/local
On 1/4/2015 6:34 AM, flebber wrote:
In repsonse to this question: Write a program that prints the first 100 members
of the sequence 2, -3, 4, -5, 6, -7, 8.
This is my solution it works but ugly.
series = range(2,100)
# answer = [(x,(y* -1)) for x, y in series[::2]]
# print(answer)
answer = []
I'm partial to:
#!/usr/local/cpython-3.4/bin/python
def gen_series(top=None):
'''
Generate the numbers from 2 to top, with alternating signs.
If top is None, generate forever.
If top is an integer, generate from 2 to top.
'''
sign = 1
if top is None:
number = 2
flebber wrote:
> In repsonse to this question: Write a program that prints the first 100
> members of the sequence 2, -3, 4, -5, 6, -7, 8.
>
> This is my solution it works but ugly.
>
> series = range(2,100)
> # answer = [(x,(y* -1)) for x, y in series[::2]]
> # print(answer)
> answer = []
> for
flebber writes:
> In repsonse to this question: Write a program that prints the first
> 100 members of the sequence 2, -3, 4, -5, 6, -7, 8.
>
> This is my solution it works but ugly.
Seems respectable to me, except that you are taking fewer than 100
elements. But I'd prefer the expressions that
^To print the first 8. To print the first 100: map(lambda i: -i if
i&1==1 else i, xrange(2, 102))
On Sun, Jan 4, 2015 at 10:47 PM, Alec Taylor wrote:
> map(lambda i: -i if i&1==1 else i, xrange(2, 10))
>
> On Sun, Jan 4, 2015 at 10:34 PM, flebber wrote:
>> In repsonse to this question: Write a p
map(lambda i: -i if i&1==1 else i, xrange(2, 10))
On Sun, Jan 4, 2015 at 10:34 PM, flebber wrote:
> In repsonse to this question: Write a program that prints the first 100
> members of the sequence 2, -3, 4, -5, 6, -7, 8.
>
> This is my solution it works but ugly.
>
> series = range(2,100)
> # a
On Sun, Jan 4, 2015 at 10:34 PM, flebber wrote:
> Just getting something wrong
> list(map((lambda x: x * -1 if (x%2 != 0)), series))
Okay, and what happens when you run this? Do you get an exception? If
so (and I fully expect you will), copy and paste the entire exception
traceback and message. T
12 matches
Mail list logo