Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8839509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:15:10+00:00 2026-06-14T10:15:10+00:00

Possible Duplicate: calling func. change the input I have to write a recursive function

  • 0

Possible Duplicate:
calling func. change the input

I have to write a recursive function that takes a list of numbers in input and returns a list of numbers in output, for example called like this:

rec_cumsum([2,2,2,3])

the output should be like:

[2,4,6,9]

Thing is, I cant seem to get my head around for this to work.. this got me questioning my whole recursive thinking..
what i have so far is :

newlist = []
k = 1
def rec_cumsum(numbers):
    if len(numbers) == 0:
        return 0
    if len(numbers) > 1 and len(numbers) != (k+1):
        newlist[k+1] == newlist[k+1] + newlist[k]
        k = k+1
        return rec_cumsum(numbers)

but I’m getting errors which doesn’t really make any sense to me.
the recursion should always take the number, and add it to the one before it, than save it in the next location of the list.. (new one or original one)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:15:11+00:00Added an answer on June 14, 2026 at 10:15 am

    I would write it like this:

    def cumulative_sum(lst,prev=0):
        if not lst:
            return []
        else:
            elem = prev+lst[0]
            return [elem] + cumulative_sum(lst[1:],prev=elem)
    
    print cumulative_sum([2,2,2,3])
    

    Now to look at your code (note I didn’t actually work through the logic to decide whether it would give the correct result, the following only addresses possible exceptions that your code is probably throwing):

    You’re probably getting an IndexError because of this line:

    newlist[k+1] == newlist[k+1] + newlist[k]
    

    You’re assigning to a list position which doesn’t exist yet. You could pre-allocate your list:

    newlist = [0]*len(lst)
    

    but even if you fix that, you’ll get a recursion error with your code because of the line:

    k = k + 1
    

    The problem here is that on the left hand side, k is local whereas on the right hand side, k is global. So essentially each time you run this, you’re getting the local k == 2 and not touching the global one. If you really want to modify the global k, you need to declare k as global via global k. Of course, then you need to reset k every time you’re going to use this function which would be a somewhat strange API.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Calling PHP Function within Javascript I have a small JavaScript code in
Possible Duplicate: Finish the calling activity when AsyncTask completes I know that I have
Possible Duplicate: Calling ActionScript 3 function from C# i have a project which is
Possible Duplicate: Calling virtual function of derived class from base class constructor? I have
Possible Duplicate: Iframe Function Calling From Iframe to parent page javascript function I have
Possible Duplicate: calling ASP function from javascript okay running this code : <script type=text/javascript>
Possible Duplicate: Calling Python from JavaScript I have a test.py and test.js . I
Possible Duplicate: Calling A Button OnClick from a function I want to do button
Possible Duplicate: why does initializing subclasses require calling the super class's same init function?
Possible Duplicate: Sizeof array passed as parameter Given the function below I understand that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.