Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

AppylPye

  • one year ago

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? http://ideone.com/x38UtA

  • This Question is Closed
  1. AlbionsRefuge
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

  2. AppylPye
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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".

  3. AlbionsRefuge
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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"!

  4. AppylPye
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Yes, that Non-track Codecademy List comprehensions course by Iryna Cherniavska is really great - "fun" - yes :)

  5. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.