Mechanical MOOC OCW Exercise 2.7 Working With Lists Solution: cumulative_sum.py
Here's a solution to the cumulative_sum exercise - 2 versions - the second function using a list comprehension. Does anyone else have a solution to post, so that we can compare different approaches?
Lists & Dictionaries
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
Thanks Appyl - I'm happy (and surprised) to report that my solution, with the exception of my variable names, is virtually the same as yours. I'm glad you explained that what the second function was doing involved list comprehensions - I'll have to pick that apart slowly.
In this statement ...
return [sum(number_list[:i + 1]) for i in range(len(number_list))]
... each index in "number_list" is considered, by virtue of the fact that "i" takes on values successively matching each index in the list, as specified by the "range" function. As this occurs, the "sum" function returns the sum of all values in the slice of "number_list" that extends from the beginning of the list, up to, and including the value at the index corresponding to the current value of "i".
Thanks for that explanation. I made it to the List comprehensions section and also worked through this http://www.codecademy.com/courses/python-beginner-en-5oLLh-btkJb -- I was surprised at what I could accomplish, and now I would almost call them "fun"!