[Chicago] A list comprehension???

Adam Forsyth adam at adamforsyth.net
Tue May 5 02:23:37 CEST 2015


Your algorithm is equivalent to

[i**2 - i for i in range(1, 11)]

because the sum from 1 to n is equal n(n + 1) / 2

That is equivalent to

[i**2 + i for i in range(10)]

because (n + 1)**2 - (n + 1) is equal to n**2 + n.

That's the simplest expression. If you simply cancel the 4s out of what
Kirby posted, you get the same thing.


On Mon, May 4, 2015 at 7:08 PM, Lewit, Douglas <d-lewit at neiu.edu> wrote:

> I've been playing with this, and I think I've got it!!!
>
> Here's what I have:
>
> [sum([2*k for k in range(i)]) for i in range(1, 11)]
>
> I started with sum(2*k for k in range(10)) and noticed that gave me 90,
> and then when you substitute range(9) then you get 72, etc.  So.... I think
> I got it!  Am I a Python genius or what?!   ;-)
>
> I'm guessing that list comprehensions, set comprehensions, generator
> comprehensions, etc, etc are used a lot in functional programming.  I have
> this ebook on functional programming in Python.  Some of it is really cool,
> and some of it is almost like rocket science to me.... but I'm learning.
>
> On Mon, May 4, 2015 at 6:58 PM, Adam Forsyth <adam at adamforsyth.net> wrote:
>
>> There is a different algorithm, one that doesn't refer to the previous
>> item in the list, that produces those numbers. Think about what kind of
>> algebraic function generally produces a series of numbers that get farther
>> and farther apart.
>>
>> On Mon, May 4, 2015 at 6:39 PM, Lewit, Douglas <d-lewit at neiu.edu> wrote:
>>
>>> Hi there,
>>>
>>> I'm reading this book, "Data Structures & Algorithms in Python" by
>>> Goodrich, Tamassia, and Goldwasser.  A pretty good book, it really does
>>> into detail about the Python language with various examples.
>>>
>>> Anyhow, one of the exercises is as follows:
>>>
>>> Demonstrate how to use Python's list comprehension syntax to produce the
>>> list:
>>> [0, 2, 6, 12, 20, 30, 42, 56, 72, 90].
>>>
>>> I'm struggling with this!
>>>
>>> The best I can do is the following:
>>>
>>> A = [0]
>>> i = 2
>>> while i <= 18:
>>>     A.append(A[-1] + i)
>>>      i+= 2
>>>
>>> print(A)
>>>
>>> Well it does work!  BUT it's not a list comprehension!
>>>
>>> Any suggestions?
>>>
>>> Thanks,
>>>
>>> Douglas.
>>>
>>> _______________________________________________
>>> Chicago mailing list
>>> Chicago at python.org
>>> https://mail.python.org/mailman/listinfo/chicago
>>>
>>>
>>
>> _______________________________________________
>> Chicago mailing list
>> Chicago at python.org
>> https://mail.python.org/mailman/listinfo/chicago
>>
>>
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> https://mail.python.org/mailman/listinfo/chicago
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20150504/2ec6dd4e/attachment-0001.html>


More information about the Chicago mailing list