Question

Write a Python program to get all possible unique subsets from a set of distinct integers....

Write a Python program to get all possible unique subsets from a set of distinct integers.
Example: If the input set of integers are: [1,2,3], then the output is:
[[], [3], [2], [2, 3], [1], [1, 3], [1, 2], [1, 2, 3]]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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 of the numbers entered are : ")
print(sub().f1(a))

Add a comment
Know the answer?
Add Answer to:
Write a Python program to get all possible unique subsets from a set of distinct integers....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a java code to solve the following question using the backtracking algorithm. Given a set...

    Write a java code to solve the following question using the backtracking algorithm. Given a set of distinct integers, return all possible subsets. input: new int[] {1,2,3} output: [], [3], [2], [2,3], [1], [1,3], [1,2], [1,2,3] What to submit: 1. Your source code in a text document (15pts), 2. A screen copy showing you can run the code and the result of running the code (5 pts). 3. What is the performance of your algorithm (5 pts), give the answer,...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list : You are given a non-decreasing sequence of n positive integers a1,a2,…,an. Print the number of distinct values in the sequence. For example, if the sequence is 1,2,2,2,3,4,4,5,7,10, the answer is 6 since distinct values are 1,2,3,4,5,7,10. Input The first line contains a positive integer n (1≤n≤1000) — the length of the sequence. The second line contains n space-separated positive integers a1,a2,…,an (1≤ai≤1000)...

  • A.) Write a Java Θ(nlogn) program that outputs all unique integers in the provided array. Output...

    A.) Write a Java Θ(nlogn) program that outputs all unique integers in the provided array. Output integers must be unique, meaning non-repetative (eg. 4, 2, 6, 3, 5, 1, 7). {4, 2, 6, 4, 3, 5, 5, 6, 6, 1, 7} B.) Provide a detailed description of the program.

  • Python 3:Write a program that inputs a text file. The program should print the unique words...

    Python 3:Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order.   Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order An example file along with the correct output is shown below: example.txt the quick brown fox jumps over the lazy dog Enter the input file name: example.txt brown dog fox jumps lazy over quick the

  • write in python Create a program that will ask the user for a list of integers,...

    write in python Create a program that will ask the user for a list of integers, all on one line separated by a slash. construct a list named "adj_numbers" which contains the users input numbers after subtracting 2 from each number. then print the resulting list. the input statement and print statement are given to you in the starting code. a sample run of the program is as follows: Enter a list of integers, separated by slashes: 7/3/6/8 resulting list:...

  • In Python 3 - Write a program that reads a sequence of integer inputs from the...

    In Python 3 - Write a program that reads a sequence of integer inputs from the user. When the user is finished entering the integers, they will enter a 'q'. There is no need to check if the entry is a valid integer. All integers in the test data (input) will be between -255 and 255. The program should then print: The smallest and largest of the inputs. The number of even and odd inputs (0 should be considered even)...

  • C++ program which partitions n positive integers into two disjoint sets with the same sum. Consider...

    C++ program which partitions n positive integers into two disjoint sets with the same sum. Consider all possible subsets of the input numbers. This is the sample Input 1 6 3 5 20 7 1 14 Output 1 Equal Set: 1 3 7 14 This is the sample Input 2 5 10 8 6 4 2 Output 2 Equal Set: 0

  • Write a program that first gets a list of integers from the input and adds them...

    Write a program that first gets a list of integers from the input and adds them to an array. The input begins with an integer indicating the number of integers that follow (Your program should work for any size of the array). Then, get the last value from the input, and output all integers less than or equal to that value by iterating through the array. Ex: If the input is: Enter the number of integers: 5 Enter integer 1:...

  • I cant get this python program to read all the unique words in a file. It...

    I cant get this python program to read all the unique words in a file. It can only read the number of the unique words in a line. import re import string from collections import Counter # opens user inputted filename ".txt" and (w+) makes new and writes def main(): textname = input("Enter the file to search: ") fh = open(textname, 'r', encoding='utf-8' ) linecount = 0 wordcount = 0 count = {} print("Sumary of the", fh) for line in...

  • In PYTHON: Write a function that receives a list of integers and returns the number of...

    In PYTHON: Write a function that receives a list of integers and returns the number of integers that are larger than the element immediately before them. For example, given [1,2,3,-5,0,-5,-10] the function should return 3 (since 1<2, 2<3, and -5<0).

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT