Why function retains values of arguments when i recall this function at the second time? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why function retains values of arguments when i recall this function at the second time?

def distribution_of(golds, l1=[], l2=[]): l1 is [] l2 is [] #print(golds) #print(l1) #print(l2) if len(golds) > 0: if len(l2) - len(l1) == 0 or len(l2) - len(l1) == 1: l1 == l1.append(max(golds[0],golds[-1])) golds == golds.remove(max(golds[0],golds[-1])) return distribution_of(golds) else: l2 == l2.append(max(golds[0],golds[-1])) golds == golds.remove(max(golds[0],golds[-1])) return distribution_of(golds) return [sum(l1), sum(l2)] print(distribution_of([4,7,2,9,5,2]))#expacted result is [11,18] print(distribution_of([10,1000,2,1]))#expacted result is [12,1001], but I get the sum of the previous result of the function and current. Why? What i passed?

7th Nov 2020, 11:52 AM
Andrey Shintar
Andrey Shintar - avatar
17 Answers
+ 2
Ok, I have verified that there is an issue with the codewars test checking itself. If you just return [0, 0] it will pass most if not all of the tests. It's a broken challenge currently.
13th Nov 2020, 9:30 PM
ChaoticDawg
ChaoticDawg - avatar
+ 12
Andrex , I have made a very simplified code to demonstrate how it works. The list res=[] in the parameters section of the function is only initialized once when the function is called the first time. so it can keep data for all following calls of the function. def testfunc(num1, num2, res=[]): res.append(num1 + num2) print(res) testfunc(1,4) testfunc(3,7)
7th Nov 2020, 12:16 PM
Lothar
Lothar - avatar
+ 7
Andrex , a first try from me, does work with the supplied sample: golds = [4,2,9,5,2,7] #golds = [10,1000,2,1] def distr_golds(golds): res = [] fin = [] while len(golds) > 0: if golds[0] > golds[-1]: res.append(golds.pop(0)) elif golds[0] < golds[-1]: res.append(golds.pop(-1)) else: res.append(golds.pop(0)) fin.append(sum([i for ndx, i in enumerate(res) if ndx % 2 == 0])) fin.append(sum([i for ndx, i in enumerate(res) if ndx % 2 != 0])) return fin print(distr_golds(golds))
14th Nov 2020, 6:38 PM
Lothar
Lothar - avatar
+ 6
Don't use mutable types as default parameters in your functions or methods. Instead set them to None and check if they are equal to None and if so set the default value.
7th Nov 2020, 11:59 AM
ChaoticDawg
ChaoticDawg - avatar
+ 6
Andrex , sorry for my late reply. I saw the issue you described, and tried to find a solution. In cases without recursion, it is no problem to get a correct result by doing small modification in code. When having a recursion, this not easy to manage. The reason is, that the content of the 2 lists should be reset, but only when entering the first recursion level, but not with the higher levels. So for me it it looks better to try a solution without recursion. Thanks for the description. I will try something and show you, but as i i am busy at the moment it can take some time.
14th Nov 2020, 4:59 PM
Lothar
Lothar - avatar
+ 6
Andrex , good to hear that it works. You also did a really good optimization of my code. Congrats 👍👍👍
14th Nov 2020, 8:56 PM
Lothar
Lothar - avatar
+ 2
def func(a, b, c=None): if c is None: c = [] .... ....
13th Nov 2020, 5:39 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Andrex I posted that link because you asked what mutation means. I assumed you meant mutable. This post explains what mutable and immutable are in python and gives some examples of types that are mutable and immutable. As for solving the codewars.com challenge for you this isn't something that the community usually does here, but we do typically help with issues in your solutions. (Errors, understanding, algorithm issues, etc). That being said here is my simple solution. I haven't tested it beyond the given tests to see if it passes. https://code.sololearn.com/cBFAGzI8RWRW/?ref=app
13th Nov 2020, 8:25 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Andrex your issue with using mutable objects was an issue, because, you used it as a default parameter. The way that I used it in my code is not an issue. There may be some edge case that my code didn't cover or the output format is incorrect etc, but it's not a mutable vs immutable issue. In fact I've swapped the code so that now the same algorithm uses immutable types (int) instead. I get the same results. This may just be an issue with this problem on codewars.
13th Nov 2020, 9:19 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
ChaoticDawg, what for? You even didn't read the description. I've already studied this topic, but i just want to solve this task still. It is necessary maybe to use tuple or indexing to solve it. Can you do it?
13th Nov 2020, 7:32 PM
Andrey Shintar
Andrey Shintar - avatar
+ 1
ChaoticDawg, Your code hasn't passed tests on codewars, like mine because you use mutation, like me. Mutation is unacceptable so. Now i want to figure out how to solve it without mutation! Do i neet to create another Discuss topic? Thanks!
13th Nov 2020, 8:38 PM
Andrey Shintar
Andrey Shintar - avatar
+ 1
I like your version, nice one! And i would short your code like this: def distr_golds(golds): res = [] fin = [] while len(golds) > 0: if golds[0] >= golds[-1]: res.append(golds.pop(0)) else: res.append(golds.pop(-1)) fin.append(sum(res[::2])) fin.append(sum(res[1::2])) return fin Thanks everyone, Lothar, ChaoticDawg! I've solved this task already and learned some new things.
14th Nov 2020, 7:51 PM
Andrey Shintar
Andrey Shintar - avatar
7th Nov 2020, 11:54 AM
Andrey Shintar
Andrey Shintar - avatar
0
Lothar, but when i assign lists after parameters section - these lists get empty because of recursion, i guess. maybe it is necessary to use two functions.
7th Nov 2020, 6:42 PM
Andrey Shintar
Andrey Shintar - avatar
0
Lothar, and how to solve it without mutation of input array? (
13th Nov 2020, 11:01 AM
Andrey Shintar
Andrey Shintar - avatar
0
ChaoticDawg, thanks! I understood what does mutation mean. But it is not what I need now. This task from codewars.com, I will be appreciated if you look out the description there. The link below: https://www.codewars.com/kata/59547688d8e005759e000092/train/JUMP_LINK__&&__python__&&__JUMP_LINK
13th Nov 2020, 6:36 PM
Andrey Shintar
Andrey Shintar - avatar