We’re rewarding the question askers & reputations are being recalculated! Read more.
5 added 11 characters in body
source | link

An assignment at school required me to write a program for this task:

  1. Create a class and define two methods in the class.
  2. Method f1 is used to pass an empty list and the sorted list is taken from the user to method f2.
  3. Method f2 is used to compute all possible subsets of the list.
  4. Then the result is returned from the function and printed.
  5. 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[0]], 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: 
[[], [5], [4], [4, 5]]

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]

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:

  1. Create a class and define two methods in the class.
  2. Method f1 is used to pass an empty list and the sorted list is taken from the user to method f2.
  3. Method f2 is used to compute all possible subsets of the list.
  4. Then the result is returned from the function and printed.
  5. 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[0]], 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: 
[[], [5], [4], [4, 5]]

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]

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:

  1. Create a class and define two methods in the class.
  2. Method f1 is used to pass an empty list and the sorted list is taken from the user to method f2.
  3. Method f2 is used to compute all possible subsets of the list.
  4. Then the result is returned from the function and printed.
  5. 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[0]], 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: 
[[], [5], [4], [4, 5]]

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]

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
3 added 68 characters in body; edited tags; edited title
source | link

An assignment at school required me to write a program for this task:

  1. Create a class and define two methods in the class.
  2. Method f1 is used to pass an empty list and the sorted list is taken from the user to method f2.
  3. Method f2 is used to compute all possible subsets of the list.
  4. Then the result is returned from the function and printed.
  5. 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[0]], 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: 
[[], [5], [4], [4, 5]]

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]

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:

  1. Create a class and define two methods in the class.
  2. Method f1 is used to pass an empty list and the sorted list is taken from the user to method f2.
  3. Method f2 is used to compute all possible subsets of the list.
  4. Then the result is returned from the function and printed.
  5. 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[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))

Here are some example outputs:

Enter number of elements of list: 2
Enter element: 4
Enter element: 5
Subsets: 
[[], [5], [4], [4, 5]]

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]

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:

  1. Create a class and define two methods in the class.
  2. Method f1 is used to pass an empty list and the sorted list is taken from the user to method f2.
  3. Method f2 is used to compute all possible subsets of the list.
  4. Then the result is returned from the function and printed.
  5. 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[0]], 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: 
[[], [5], [4], [4, 5]]

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]

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
1
source | link