Today we are going to share a Python Program to Create a Class and Get All Possible Subsets from a Set of Distinct Integers. If you are a python beginner and want to start learning the python programming, then keep your close attention in this tutorial as I am going to share a Python Program to Create a Class and Get All Possible Subsets from a Set of Distinct Integers.
To increase your Python knowledge, practice all Python programs, here is a collection of 100+ Python problems with solutions.
Copy the below python program and execute it with the help of python compiler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class sub: def f1(self, s1): return self.f2([], sorted(s1)) def f2(self, curr, s1): if s1: return self.f2(curr, s1[1:]) + self.f2(curr + [s1[0]], s1[1:]) return [curr] a=[] n=int(input("Enter number of elements of list: ")) for i in range(0,n): b=int(input("Enter element: ")) a.append(b) print("Subsets: ") print(sub().f1(a)) |
Enter number of elements of list: 4
Enter element: 3
Enter element: 5
Enter element: 7
Enter element: 28
Subsets:
[[], [28], [7], [7, 28], [5], [5, 28], [5, 7], [5, 7, 28], [3], [3, 28], [3, 7], [3, 7, 28], [3, 5], [3, 5, 28], [3, 5, 7], [3, 5, 7, 28]]
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.