From 021dad51e78a66f2d46301371c59e138946cca90 Mon Sep 17 00:00:00 2001 From: KDH Date: Tue, 5 Nov 2024 14:03:04 +0900 Subject: [PATCH] [Leetcode] sum-of-all-subset-xor-totals --- .../sum_of_all_subset_xor_totals/__init__.py | 0 .../sum_of_all_subset_xor_totals/test.py | 16 ++++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 coding_test/leetcode/sum_of_all_subset_xor_totals/__init__.py create mode 100644 coding_test/leetcode/sum_of_all_subset_xor_totals/test.py diff --git a/coding_test/leetcode/sum_of_all_subset_xor_totals/__init__.py b/coding_test/leetcode/sum_of_all_subset_xor_totals/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/coding_test/leetcode/sum_of_all_subset_xor_totals/test.py b/coding_test/leetcode/sum_of_all_subset_xor_totals/test.py new file mode 100644 index 0000000..07051ef --- /dev/null +++ b/coding_test/leetcode/sum_of_all_subset_xor_totals/test.py @@ -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