0

I am trying to print the sum of all the subsets of an array using recursion.. But I am only getting one sum and the scope of the variable inside my recursive function is losing, because of that next recursion logic is failing or my complete recursive logic is not proper...

Note: May be I am not able to explain my approach well in the description.. but you can understand what I am trying to do by looking at the code..

The variable localTempArray is losing the scope after first cycle of recursion..and I am using it to make another recursive call

Please help me to understand recursion well...

Thanks

import sys

a = [2,4,8,16,32,64,128,256,512,1024]
print len(a)
def arrayPop(temp,i):
    try:
        temp.pop(i)
        return temp
    except:
        return temp
def recursiveSum(tempArray):
    localTempArray = tempArray
    print localTempArray
    if (len(tempArray) == 0):
        return 0
    ybinarysum = localTempArray[0] + recursiveSum(arrayPop(tempArray,0))
    print localTempArray
    recursiveSum(arrayPop(localTempArray,0))
    return ybinarysum

recursiveSum(a)

Current Output:

10
[2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
[4, 8, 16, 32, 64, 128, 256, 512, 1024]
[8, 16, 32, 64, 128, 256, 512, 1024]
[16, 32, 64, 128, 256, 512, 1024]
[32, 64, 128, 256, 512, 1024]
[64, 128, 256, 512, 1024]
[128, 256, 512, 1024]
[256, 512, 1024]
[512, 1024]
[1024]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
5
  • localTempArray = tempArray does NOT make a separate copy of the array. Both names refer to the same object. Commented Jan 23, 2019 at 4:54
  • ok, but the scope of the variables of a particular function should be retain the same after the recursion right..? Commented Jan 23, 2019 at 4:58
  • 1
    import copy; local_temp_array = copy.copy(tempArray) Commented Jan 23, 2019 at 5:00
  • Is it like only in python we have to do this or its just same in every programing language, bcz I feel in js it works in a different way.. Commented Jan 23, 2019 at 5:10
  • anyway thanks.. it solved my problem.. but my logic is completely wrong..have to find new way... Thanks again... Commented Jan 23, 2019 at 5:11

2 Answers 2

2

Since list is mutatable, the assignment localTempArray = tempArray , localTempArray is just a reference to same memory location as of tempArray, so any modification with localTempArray will be reflected.

check this vizualization here

You can use copy module,

from copy import copy
localTempArray = copy(tempArray)
Sign up to request clarification or add additional context in comments.

Comments

0

Lists in python are mutable type. localTempArray and tempArray refer to the same objects.

When you pop localTempArray, tempArray will be popped too.

Anyway, if you wanna get a sub array of tempArray excepted the element 0. Try this

ybinarysum = localTempArray[0] + recursiveSum(localTempArray[1:])

1 Comment

Oh.. Thanks for the info...looks like it's working fine after replacing with your logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.