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:

Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
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.
Python: Tips of the Day
Free up Memory:
- Manual garbage collection can be performed on timely or event based mechanism.
import gc collected_objects = gc.collect()
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook