Question

In: Computer Science

Write a program in python that reads the elements of a set from the keyboard, stores...

Write a program in python that reads the elements of a set from the keyboard, stores them in a set, and then determines its powerset. Specifically, the program should repeatedly ask the user:

Enter one more element ? [Y/N]

If the user answers Y then an new element is read from the keyboard:

Enter the new element in the set:

This cycle continues until the user answers N to the first question. At that point the program shall compute the powerset of the set of elements provided by the user, and print it to the screen. The powerset should not only be printed to the screen, but also stored in an instance of set.

Important: for what stated above, the elements of the powerset cannot be sets themselves. It is ok to store them in tuples. Note that while you can use a list to create a set (like in the example where we used list of chars), you cannot store lists in a set because of the same reason you cannot store sets inside a set. For example, the following code will give you an error

>>> A = [1,2] >>> B = [3,4] >>> C = [A,B] >>> D = set(C) 3 ---------------------------------------------------------------------------

TypeError Traceback (most recent call last) in ----> 1 D = set(C)

TypeError: unhashable type: ’list’

The reason is that D = set(C) tries to build a set of elements from the list C, and the elements of C are the lists A and B. On the contrary, E = set(A) would be ok, because it builds a set from the elements of A, and the elements of A are integers.

Solutions

Expert Solution

Code:

# input set
s = set()
while(True):
    option = input('Enter one more element ? [Y/N] ')
    if(option == 'N'):
        break
    val = int(input('Enter the new element in the set: '))
    s.add(val)

powerset = set()
val = list(s) # convert input set s to list

n = len(val) # number of elements

# generate powerset using bitmasking
for i in range(1<<n):
    curr = tuple([val[j] for j in range(n) if(i & (1<<j))]) # generate as tuple
    powerset.add(curr) # add to powerset

# print the powerset
print(powerset)

Code screenshot:

code output

===========

The code along with comments and screenshot has been added. Bitmasking has been used to generate the powerset.

Please upvote.


Related Solutions

Design and write a python program that reads a file of text and stores each unique...
Design and write a python program that reads a file of text and stores each unique word in some node of binary search tree while maintaining a count of the number appearance of that word. The word is stored only one time; if it appears more than once, the count is increased. The program then prints out 1) the number of distinct words stored un the tree, Function name: nword 2) the longest word in the input, function name: longest...
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
Write a program that reads three values from the keyboard representing, respectively, an investors name, an...
Write a program that reads three values from the keyboard representing, respectively, an investors name, an investment amount, and an interest rate (you will expect the user to enter a number such as .065 for the interest rate, representing a 6.5% interest rate) . Your program should calculate and output (in currency notation) the future value of the investment in 10, 2 20 an 30 years using the following formula:   Future value =investment(1+interest rate)year Example of a test run: Enter...
Using C++ Write a program that reads a text from the keyboard until the user presses...
Using C++ Write a program that reads a text from the keyboard until the user presses the “Enter” key. Your program must count the number of uppercase alphabets (A through Z), lowercase alphabets (a through z), digits (0 through 9) and any other characters. The other character count value should NOT include the “Enter” key. Display the count on the screen. You must use the in-built C++ character I/O functions in this program.
Question 1: Write a C program that reads a date from the keyboard and tests whether...
Question 1: Write a C program that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid month...
write a python program that inputs 10 integer values from the keyboard and then displays their...
write a python program that inputs 10 integer values from the keyboard and then displays their sum. use for loop
In Python, write a program to get a positive integer n from keyboard first (your program...
In Python, write a program to get a positive integer n from keyboard first (your program must be able to check the validation of n being positive), and then get n integers from keyboard and store these n integers in a list. Do some processing, and print out average of the elements in the list at end of your program. For example, suppose user enters 3 first (n = 3), which means we get another three integers from the user....
Write a program that reads three double numbers from the keyboard representing, respectively, the three coefficients...
Write a program that reads three double numbers from the keyboard representing, respectively, the three coefficients a, b, and c of a quadratic equation. Solve the equation using the following formulas: x1 = ( - b + square root (b2 – 4ac)) / (2a), x2 = ( - b + square root (b2 – 4ac)) / (2a) Run your program on the following sample values: a=1.0, b=3.0,c=2.0 a=0.5, b=0.5,c=0.125 a=1.0, b=3.0,c=10.0
Write a program that reads numbers from scanf1 (keyboard) and then sums them, stopping when 0...
Write a program that reads numbers from scanf1 (keyboard) and then sums them, stopping when 0 has been entered. Construct three versions of this program, using the while, do-while, and for loops.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT