# Python Program to Create a Class and Get All Possible Distinct Subsets from a Set

This is a Python Program to create a class and get all possible subsets from a set of distinct integers.

Problem Description

The program creates a class to get all possible subsets from a set of distinct integers.

Problem Solution

1. Create a class and define two methods in the class.
3. Method f1 is used to pass an empty list and the sorted list taken from the user to method f2.
4. Method f2 is used to compute all possible subsets of the list.
5. Then the result is returned from the function and printed.
6. Exit

Program/Source Code

Here is the source code of the Python Program to create a class to get all possible subsets from a set of distinct integers. The program output is also shown below.

```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))```
Program Explanation

1. A class called sub is created and two methods f1 and f2 are defined.
3. In main, the number of elements is taken from the user.
3. Using a for loop, the elements are taken from the user and appended to an empty list.
5. Then, the method f1 of the class sub is called by passing the list taken from the user as the parameter.
6. Method f1 in turn calls method f2 with an empty list and the sorted list taken from the user as parameters.
7. Method f2 is a recursive function.
8. This method computes all the possible subsets by splitting the current list part by part starting from an empty list and updating the value of the current list.
9. This continues till the list becomes empty.
10. The current value of the list which is a list containing subsets as separate lists is returned.
11. The final result is printed.

Runtime Test Cases
```
Case 1:
Enter number of elements of list: 2
Enter element: 4
Enter element: 5
Subsets:
[[], [5], [4], [4, 5]]

Case 2:
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]]```

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Note: Join free Sanfoundry classes at Telegram or Youtube

If you find any mistake above, kindly email to [email protected]