We’re rewarding the question askers & reputations are being recalculated! Read more.

 5 added 11 characters in body edited May 21 at 18:05 Justin 2,21266 silver badges3333 bronze badges An assignment at school required me to write a program for this task: Create a class and define two methods in the class. Method f1 is used to pass an empty list and the sorted list is taken from the user to method f2. Method f2 is used to compute all possible subsets of the list. Then the result is returned from the function and printed. Exit Here is my solution to this task, using Python: class sub: def f1(self, s1): return self.f2([], sorted(s1)) def f2(self, current, s1): if s1: return self.f2(current, s1[1:]) + self.f2(current + [s1], s1[1:]) return [current] 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))  NOTE - Method f2 is a recursive function. Here are some example inputs and outputs: Enter number of elements of list: 2 Enter element: 4 Enter element: 5 Subsets: [[], , , [4, 5]] Enter number of elements of list: 4 Enter element: 3 Enter element: 5 Enter element: 7 Enter element: 28 Subsets: [[], , , [7, 28], , [5, 28], [5, 7], [5, 7, 28], , [3, 28], [3, 7], [3, 7, 28], [3, 5], [3, 5, 28], [3, 5, 7], [3, 5, 7, 28]  So, I would like to know whether I could make this program shorter and more efficient. Any help would be highly appreciated. An assignment at school required me to write a program for this task: Create a class and define two methods in the class. Method f1 is used to pass an empty list and the sorted list is taken from the user to method f2. Method f2 is used to compute all possible subsets of the list. Then the result is returned from the function and printed. Exit Here is my solution to this task, using Python: class sub: def f1(self, s1): return self.f2([], sorted(s1)) def f2(self, current, s1): if s1: return self.f2(current, s1[1:]) + self.f2(current + [s1], s1[1:]) return [current] 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))  NOTE - Method f2 is a recursive function. Here are some example outputs: Enter number of elements of list: 2 Enter element: 4 Enter element: 5 Subsets: [[], , , [4, 5]] Enter number of elements of list: 4 Enter element: 3 Enter element: 5 Enter element: 7 Enter element: 28 Subsets: [[], , , [7, 28], , [5, 28], [5, 7], [5, 7, 28], , [3, 28], [3, 7], [3, 7, 28], [3, 5], [3, 5, 28], [3, 5, 7], [3, 5, 7, 28]  So, I would like to know whether I could make this program shorter and more efficient. Any help would be highly appreciated. An assignment at school required me to write a program for this task: Create a class and define two methods in the class. Method f1 is used to pass an empty list and the sorted list is taken from the user to method f2. Method f2 is used to compute all possible subsets of the list. Then the result is returned from the function and printed. Exit Here is my solution to this task, using Python: class sub: def f1(self, s1): return self.f2([], sorted(s1)) def f2(self, current, s1): if s1: return self.f2(current, s1[1:]) + self.f2(current + [s1], s1[1:]) return [current] 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))  NOTE - Method f2 is a recursive function. Here are some example inputs and outputs: Enter number of elements of list: 2 Enter element: 4 Enter element: 5 Subsets: [[], , , [4, 5]] Enter number of elements of list: 4 Enter element: 3 Enter element: 5 Enter element: 7 Enter element: 28 Subsets: [[], , , [7, 28], , [5, 28], [5, 7], [5, 7, 28], , [3, 28], [3, 7], [3, 7, 28], [3, 5], [3, 5, 28], [3, 5, 7], [3, 5, 7, 28]  So, I would like to know whether I could make this program shorter and more efficient. Any help would be highly appreciated. 4 edited tags | link edited May 21 at 8:15 Justin 2,21266 silver badges3333 bronze badges 3 added 68 characters in body; edited tags; edited title edited May 21 at 7:46 Justin 2,21266 silver badges3333 bronze badges An assignment at school required me to write a program for this task: Create a class and define two methods in the class. Method f1 is used to pass an empty list and the sorted list is taken from the user to method f2. Method f2 is used to compute all possible subsets of the list. Then the result is returned from the function and printed. Exit Here is my solution to this task, using Python: class sub: def f1(self, s1): return self.f2([], sorted(s1)) def f2(self, currcurrent, s1): if s1: return self.f2(currcurrent, s1[1:]) + self.f2(currcurrent + [s1], s1[1:]) return [curr][current] a=[]a = [] n=intn = int(input("Enter number of elements of list: ")) for i in range(0,n): b=intb = int(input("Enter element: ")) a.append(b) print ("Subsets: ") print (sub().f1(a))  NOTE - Method f2 is a recursive function. Here are some example outputs: Enter number of elements of list: 2 Enter element: 4 Enter element: 5 Subsets: [[], , , [4, 5]] Enter number of elements of list: 4 Enter element: 3 Enter element: 5 Enter element: 7 Enter element: 28 Subsets: [[], , , [7, 28], , [5, 28], [5, 7], [5, 7, 28], , [3, 28], [3, 7], [3, 7, 28], [3, 5], [3, 5, 28], [3, 5, 7], [3, 5, 7, 28]  So, I would like to know whether I could make this program shorter and more efficient. Any help would be highly appreciated. An assignment at school required me to write a program for this task: Create a class and define two methods in the class. Method f1 is used to pass an empty list and the sorted list is taken from the user to method f2. Method f2 is used to compute all possible subsets of the list. Then the result is returned from the function and printed. Exit Here is my solution to this task, using Python: 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], 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))  Here are some example outputs: Enter number of elements of list: 2 Enter element: 4 Enter element: 5 Subsets: [[], , , [4, 5]] Enter number of elements of list: 4 Enter element: 3 Enter element: 5 Enter element: 7 Enter element: 28 Subsets: [[], , , [7, 28], , [5, 28], [5, 7], [5, 7, 28], , [3, 28], [3, 7], [3, 7, 28], [3, 5], [3, 5, 28], [3, 5, 7], [3, 5, 7, 28]  So, I would like to know whether I could make this program shorter and more efficient. Any help would be highly appreciated An assignment at school required me to write a program for this task: Create a class and define two methods in the class. Method f1 is used to pass an empty list and the sorted list is taken from the user to method f2. Method f2 is used to compute all possible subsets of the list. Then the result is returned from the function and printed. Exit Here is my solution to this task, using Python: class sub: def f1(self, s1): return self.f2([], sorted(s1)) def f2(self, current, s1): if s1: return self.f2(current, s1[1:]) + self.f2(current + [s1], s1[1:]) return [current] 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))  NOTE - Method f2 is a recursive function. Here are some example outputs: Enter number of elements of list: 2 Enter element: 4 Enter element: 5 Subsets: [[], , , [4, 5]] Enter number of elements of list: 4 Enter element: 3 Enter element: 5 Enter element: 7 Enter element: 28 Subsets: [[], , , [7, 28], , [5, 28], [5, 7], [5, 7, 28], , [3, 28], [3, 7], [3, 7, 28], [3, 5], [3, 5, 28], [3, 5, 7], [3, 5, 7, 28]  So, I would like to know whether I could make this program shorter and more efficient. Any help would be highly appreciated. 2 Reword question title, removed performance tag, added homework tag | link edited May 21 at 7:44 AlexV 5,56022 gold badges1414 silver badges3939 bronze badges 1 asked May 21 at 7:42 Justin 2,21266 silver badges3333 bronze badges