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

  • SEARCH
  • Home
  • 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 8819623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:24:26+00:00 2026-06-14T05:24:26+00:00

I am writing some recursive function. Consider its prototype to be: def path(a, row=0,

  • 0

I am writing some recursive function. Consider its prototype to be:

def path(a, row=0, col=0, weight=0, cumulative=[])

I am trying to find some path in the matrix a. The weight adds the values of that path so far and cumulative keeps track of the path.

At some point, this is invoked:

return path(a, row+1, col, weight, cumulative) + path(a, row+1, col+1, weight, cumulative)

Now the base case is as follows:

cumulative.append(a[row][col])
weight = weight + a[row][col]
return weight

which adds the current matrix element into cumulative, adds the weight and returns the weight. The problem is that each time cumulative calls append, it adds to the very same instance of cumulative. How can I ensure that each recursive stack frame takes a different copy of cumulative?

So when append is called in the base case, I want the value of cumulative called to be of the frame caller and not the previous recursive stack frame.

Any suggestions? Thanks.

  • 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-14T05:24:27+00:00Added an answer on June 14, 2026 at 5:24 am

    you could call with a copy of cumulative:

    return path(a, row+1, col, weight, list(cumulative)) + 
           path(a, row+1, col+1, weight, list(cumulative))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a recursive function in JS and having some trouble. Let's start with
While writing some views to respond to ajax requests i find it somewhat strange
I'm writing some code that will take a screenshot of another application, given its'
I am writing the C# function which retrieves some files matched pattern. Input :
I've been writing some recursive ascent parsers, and one of the things I've been
I'm writing a function that finds the full path of a directory based on
I am writing a recursive function that has two functions, one to add numbers
While writing some C code, I decided to compile it to assembly and read
Im writing some helper functions for a project im working on. I've always wanted
Just writing some CSS and I have a pretty cool Search form where you

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.