Home > Software design >  1013. Partition Array Into Three Parts With Equal Sum
1013. Partition Array Into Three Parts With Equal Sum

Time:12-20

I am working on Leetcode problem 1013. I found the solution that just one line, but I can not understand the conception behind the code.

import itertools
A = [0,2,1,-6,6,-7,9,1,2,0,1]
def canThreePartsEqualSum(A):
    return (lambda x,y: x in y and 2*x in y and 3*x in y)(sum(A)//3,itertools.accumulate(A))
print(canThreePartsEqualSum(A))

hope some of you can describe it detailedly for me, thanks ><

CodePudding user response:

The code first imports the itertools module, which provides a number of functions for working with iterators.

Then, the function canThreePartsEqualSum(A) is defined, which takes a list A as an input and returns a boolean value indicating whether the list can be divided into three parts such that the sum of each part is equal.

Inside the function, a lambda function is defined and returned. This lambda function takes two arguments: x and y. The lambda function checks whether the following conditions are true:

  1. x is present in y (i.e., x is an element of y)
  2. 2*x is present in y
  3. 3*x is present in y The lambda function is passed two arguments: sum(A)//3 and itertools.accumulate(A). The sum(A)//3 expression calculates the sum of the elements in A, then divides it by 3 and returns the integer result. The itertools.accumulate(A) function returns an iterator that produces the cumulative sum of the elements in A, starting with the first element.

Finally, the canThreePartsEqualSum(A) function is called with the list A as an argument, and the result is printed.

For example, if the list A is [0,2,1,-6,6,-7,9,1,2,0,1], the sum of the elements in A is 10, so sum(A)//3 is 3. The itertools.accumulate(A) function will return an iterator that produces the following sequence: [0, 2, 3, -3, 3, -4, 5, 6, 8, 8, 9]. The lambda function will check whether 3, 6, and 9 are elements of this sequence, which they are. Therefore, the lambda function will return True, and the canThreePartsEqualSum(A) function will return True. The output of the print() function will be True.

CodePudding user response:

I'll try to break down each part of this

sum(A) = 9

sum(A)//3 = 9//3 = 3 (This is floor division)

itertools.accumulate(A) - returns an accumulative total of the passed iterable In this case, it would be [0, 2, 3, -3, 3, -4, 5, 6, 8, 8, 9]

(sum(A)//3,itertools.accumulate(A)) - this is a tuple. It is effectively (3, [0, 2, 3, -3, 3, -4, 5, 6, 8, 8, 9]) but if you tried to print this it would just give you (3, <itertools.accumulate object>)

(lambda x,y: x in y and 2*x in y and 3*x in y) - This is a lambda function and it can be rather confusing to look at for beginners, it is basically an anonymous function. In this case, it is checking if x (which is the first element in the tuple(sum(A)//3,itertools.accumulate(A)) so in this case it's a 3) is in the accumulate object and whether 2*3 and 3*3 is also in that list.

If all conditions are met it will return True, otherwise False.

  • Related