Become a leader in the IoT community!
Join our community of embedded and IoT practitioners to contribute experience, learn new skills and collaborate with other developers with complementary skillsets.
Join our community of embedded and IoT practitioners to contribute experience, learn new skills and collaborate with other developers with complementary skillsets.
What will be the output of this code?
def count_subsets(nums, target):
memo = {}
def backtrack(index, remaining):
if (index, remaining) in memo:
return memo[(index, remaining)]
if remaining == 0:
return 1
if remaining < 0 or index == len(nums):
return 0
count = backtrack(index + 1, remaining - nums[index]) + backtrack(index + 1, remaining)
memo[(index, remaining)] = count
return count
return backtrack(0, target)
nums = [1, 2, 3, 4, 5]
target = 7
print(count_subsets(nums, target))
Browse other Product Reviews tagged
It’s 2
The function counts the number of subsets coming from the `num` array , which sums up to the `target` value . Which would definitely be 2
Try again
For reals? π
yes yes yes
But there are two subsets [1, 2, 4] and [3, 4] whose elements sum up to 7 , so it’s 2
No , itβs 3 π
{1, 2, 4}, {2, 5}, and {3, 4}.
You’re only missing one
I was that close
yeah π β¨
Brilliant
CONTRIBUTE TO THIS THREAD