Python: Get all possible unique subsets from a set of distinct integers
Python Class: Exercise-4 with Solution
Write a Python program to get all possible unique subsets from a set of distinct integers.
Sample Solution:
Python Code:
class py_solution:
def sub_sets(self, sset):
return self.subsetsRecur([], sorted(sset))
def subsetsRecur(self, current, sset):
if sset:
return self.subsetsRecur(current, sset[1:]) + self.subsetsRecur(current + [sset[0]], sset[1:])
return [current]
print(py_solution().sub_sets([4,5,6]))
Sample Output:
[[], [6], [5], [5, 6], [4], [4, 6], [4, 5], [4, 5, 6]]
Pictorial Presentation:
Flowchart:
Python Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Python program to find validity of a string of parentheses, '(', ')', '{', '}', '[' and ']. These brackets must be close in the correct order,
for example "()" and "()[]{}" are valid but "[)", "({[)]" and "{{{" are invalid.
Next: Write a Python program to find a pair of elements (indices of the two numbers) from a given array whose sum equals a specific target number.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/class-exercises/python-class-exercise-4.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics