# [Chicago] A list comprehension???

Lewit, Douglas d-lewit at neiu.edu
Wed May 6 06:32:22 CEST 2015

```Hi Phillip,

Thanks for the reply.  It was a tricky problem for sure, and I agree with
you.  Often when you read a computer science textbook, mathematics
textbook, physics textbook, etc, etc, you really have to be a little
psychic and try to figure out what the author was really thinking.  And of
know, and sometimes those assumptions are not realistic.

I love your approach!  It's a really nice combination of math and computer
programming.  As long as you have two initial conditions, the differential
equation y'' = 2 is extremely easy to solve.

On a slightly different topic, I visited the Adler Planetarium on
Saturday.  Wow!  A lot of fun.  I chatted with this young lady who's
currently working on her PhD. in Physics at UIC.  She was the narrator for
one of the sky shows.  I asked her what software or programming language is
currently very popular among physicists for simulations, problem solving,
etc.  Not surprisingly, she replied PYTHON!  But Python is not alone in
that grab bag.  Physicists are also very fond of Fortran.  She told me that
most of the computer work in her department was done in Fortran, Python,
Mathematica and Matlab, with Python picking up at least 50% of that.
Interesting for sure.

Thanks for the feedback.

On Tue, May 5, 2015 at 5:54 PM, Robare, Phillip (Randstant) <
proba at allstate.com> wrote:

>  I missed this thread yesterday, but here is my 2 cents on how you might
> go about solving the problem the author of your text gave.
>
>
>
> The problem does not state what the generator of the sequence is, which
> seems a bit unfair from a programming text. So the first problem is to find
> a mathematical expression that given an input (of a position) will output
> the value.
>
>
>
> A common form of many problems of this type is that the differences follow
> some pattern.  So create a table of differences.
>
>
>
> 0
>
>                 2
>
> 2                              2
>
>                 4
>
> 6                              2
>
>                 6
>
> 12                           2
>
>                 8
>
> 20                           2
>
>                 10
>
> 30                           2
>
>                 12
>
> 42                           2
>
>                 14
>
> 56                           2
>
>                 16
>
> 72                           2
>
>                 18
>
> 90
>
>
>
> So we have a constant difference for the second difference.  This can be
> expressed as the differential equation
>
>
>
> y’’ = 2
>
>
>
> If you want to exercise your calculus knowledge you can integrate this, or
> you can go to http://www.wolframalpha.com/ and input exactly that line.
>
>
>
> Either way you get the equation
>
>
>
> y = x^2 + C1*x + C2
>
>
>
> Since the list in the exercise has y(0) = 0 then C2 is 0.
>
> y(1) in the list is 2 so we substitute into the equation and solve for C1
>
>
>
> 2 = 1^2 + C1 * 1 + 0
>
> 2 = 1 + C1
>
>
>
> So C1 is 1 and our equation is
>
> y = x^2 + x
>
>
>
> or equivalently
>
> y = x * (x + 1)
>
>
>
> Now I bet if the book had asked you to create a list using list
> comprehensions  for the first 10 natural numbers for this equation you
> would have not had any trouble.  So the problem is not learning how to
> program, the program is how to parse the problem so that you understand
> that first you have to come up with an equation to stick in the proper
> place in the list comprehension.
>
>
>
> Even in elementary school I remember feeling that the math problems were
> not problems in math, but problems in determining what the author was
> thinking when they wrote the question.  In real work the source of the
> sequence that I am manipulating is always known or there is someone who I
> can ask about it.  I have never gotten a real world problem where the
> inputs and outputs were specified without any idea of the processing needed.
>
>
>
> So although this question created a nice discussion thread: I say, as a
> programming exercise, the statement of the problem is flawed.
>
>
>
>
>
>
>
> Phil Robare
>
> TEK Systems / Allstate QR&A
>
> 847-667-0431
>
> D2D 82-O
>
>
>
> *From:* Chicago [mailto:chicago-bounces+proba=allstate.com at python.org] *On
> Behalf Of *Lewit, Douglas
> *Sent:* Monday, May 04, 2015 6:39 PM
> *To:* The Chicago Python Users Group
> *Subject:* [Chicago] A list comprehension???
>
>
>
> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20150505/c8460587/attachment.html>
```