Skip to content

Commit

Permalink
[Leetcode] sum-of-all-subset-xor-totals
Browse files Browse the repository at this point in the history
  • Loading branch information
ehdgua01 committed Nov 5, 2024
1 parent 073136d commit 021dad5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
Empty file.
16 changes: 16 additions & 0 deletions coding_test/leetcode/sum_of_all_subset_xor_totals/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import functools
import operator
from itertools import combinations
from typing import List


class Solution:
def subsetXORSum(self, nums: List[int]) -> int:
return sum(functools.reduce(operator.xor, c) for i in range(1, len(nums) + 1) for c in combinations(nums, i))


def test_subsetXORSum():
s = Solution()
assert s.subsetXORSum([1, 3]) == 6
assert s.subsetXORSum([5, 1, 6]) == 28
assert s.subsetXORSum([3, 4, 5, 6, 7, 8]) == 480

0 comments on commit 021dad5

Please sign in to comment.