Question

for Power Set can I have it written in python with no hard coded data and...

for Power Set can I have it written in python with no hard coded data and it must receive data from the user .

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

PROGRAM CODE :

import math #importing python math package

#defining Python Function Named findPowerSet with arguments user entered SET and SIZE of the set

def findPowerSet(set,setSize) :

#Setting the size of the Power Set of the set with setSize n is (2**n -1).Empty Set won't be printed

powerSetSize = (int) (math.pow(2, setSize))

#defining counter variable and setting its value equals to 0

counter = 0

#defining a j variable and setting its value equals to 0

j = 0

print("\nPOWER SET : ")

#incrementing the counter value from 0 to the size of the POWER SET

for counter in range(0, powerSetSize) :

#incrementing the j value from 0 to size of the user entered SET

for j in range(0, setSize) :

#Check if jth bit in the counter is set. If set then print jth element from SET

if((counter & (1 << j)) > 0) :

#printing the jth element from the SET

print(set[j], end = "")

#printing the new line

print("")

#prompting the user to set the elements of the SET separated by COMMAS

inputSet = input("\nENTER ELEMENTS OF THE SET SEPARATED BY COMMAS : ")

#splitting the SET ELEMENTS on the basis of the COMMAS and storing it into the SET list

set = inputSet.split(",")

#finding the size of the SET

size = len(set)

#printing the user entered SET after converting it into String

print("\nSET ENTERED: "+str(set))

#calling the function findPowerSet with the SET list and size of the SET list

findPowerSet(set,size)

PROGRAM CODE :

The above program is written in Python Programming Language using Notepad++ and is saved as PowerSet.py

As it is a Python Program, it has to be saved with .py extension

The Python Version used is Python 3.7.0

CAUsers Abhijith DesktoplPython)PowerSet.py - Notepad++ File Edit Search View Encoding Language Settings Tools Macro Bun Plugins Window Powerset py import nath importing python math peckage #defining Python Function Named findPoverset vith arguments user entered SET and SIZE of the set Setting the size of the Pover Set of the set rith setSize n is (2n -1).Empty Set ont be printed idefining counter variable and setting its value cquals to 0 idefining a j ariable and setting its value quals to 0 def findPowerSet (set,setSize) powerSetSize -(int) (nath.pow (2, setSize)) counter-0 10 print(nPONER SET 12 13 14 15 16 fincrenenting the counter value Iron o to the sie of the POWER SET fincrenenting the j value rom 0 to size or the user entered SEI for counter in range (0, powerSetsize) for j in range (, setS12e) fCheck ir jth bit in the counter is set Ir set then print jta element rrom SET fprinting the jth elenent fron the SEr #printing the nev line 1f ((counter & (1 <<))) > 0): print (set [j], end= ) 18 19 20 21 print( inputSet = input(,\nENTER ELEMENTS OF THE SET SEPARATED BY COMMAS · ) set inputSet. split(,) size = len(set) print( nSET ENTERED: +str (set)) findPowerSet (set,size) prompting the user to set the elements of the SET separated by ca Nas #splitting the SET ELEMENTS on the basis of the COMAS and storing it into the S T 1ist #finding the size of the SET #printing the user enterd SET after converting it into String 23 24 with the SET list and size of the Python file length: 1,517 lines:24 Ln:24 Col: 130 Sel: 010 Windows (CRLF) UTF-8 INS 15:32 E ENG 11-12-18

OUTPUT :

The above program is compiled and executed in the Command Prompt using the command python PowerSet.py

The syntax for compiling and executing the Python Program is python ProgramName.py

The output is as follows

Add a comment
Know the answer?
Add Answer to:
for Power Set can I have it written in python with no hard coded data and...
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 program (python) that multiplies two hard-coded numbers, then prints the individual numbers out and...

    Write a program (python) that multiplies two hard-coded numbers, then prints the individual numbers out and prints their product out, all on separate lines. You can choose the two numbers that are multiplied together. Make a copy of previous exercise then modify it to print output exactly like this, rather than just the numbers on separate lines: 10 x 7 = 70 Do this using the string.format() function discussed in the lecture.

  • Hi, I need a program written in Python that can translate a phone number into letters....

    Hi, I need a program written in Python that can translate a phone number into letters. The program needs to prompt the user to input a phone number of their choice to translate. I'm in a basic level python class so I would appreciate it if the program wasn't too high skilled or included "char" if possible. Thank you in advance.

  • It written in can be python or java There is a bookstore that have the title,...

    It written in can be python or java There is a bookstore that have the title, year, price is should display all the books in the collection when given the list of books some wants to purchase it will return the total amount they need to pay for example with a list of books like: great expectations, 1861, 13.21 wind in the willows, 1999, 5.89 great gatsby ,1914,20.90 it should show a list of the books at the start when...

  • I have to write this in Python and it must run in Python 3. The last...

    I have to write this in Python and it must run in Python 3. The last two pictures are what I have so far please correct me if anything I have written so far is wrong. anything interesting 6.8. Do addition Summary Now you will replace the pass statement inside the addition case of the iflelif ladder with something that actually does addition. Since the only number we have right now is the accumulator, you will need to ask the...

  • Written in python programming for MyProLab The elements of a tuple can be set to 0...

    Written in python programming for MyProLab The elements of a tuple can be set to 0 recursively as follows: If the tuple has a length of zero, nothing should be done to it. Otherwise, set the first element of the tuple to 0, and then append it to the rest of the tuple. Write a function named clearthat accepts one argument, a tuple of any length, and returns a tuple of the same length with every element set to 0.

  • Java. I am having a hard time figuring out how to take user input and then...

    Java. I am having a hard time figuring out how to take user input and then write a method that returns the answer. All the examples I find have numbers hard coded. Can you show me how this is done. For example, maybe add two numbers. Thank you

  • Please have it written in Python. The distance a vehicle travels can be calculated as follows:...

    Please have it written in Python. The distance a vehicle travels can be calculated as follows: distance 5 speed 3 time For example, if a train travels 40 miles per hour for three hours, the distance traveled is 120 miles. Write a program that asks the user for the speed of a vehicle (in miles per hour) and the number of hours it has traveled. It should then use a loop to display the distance the vehicle has traveled for...

  • There are two last hard written examinations for statistics this term, the Problem Set and the...

    There are two last hard written examinations for statistics this term, the Problem Set and the Final Exam. If just in case the Problem Set scores are on a scale of 1 to 36. The distribution of Problem Set scores are approximately Normal with mean μ = 21.5 and standard deviation σ = 5.4. Final exam scores are reported on a scale from 60 to 240. The Final Exam scores are also normally distributed with a mean μ = 150.9...

  • JAVA (Student Poll) Figure 7.8 contains an array of survey responses that’s hard coded into the...

    JAVA (Student Poll) Figure 7.8 contains an array of survey responses that’s hard coded into the program. Suppose we wish to process survey results that are stored in a file. This exercise requires two separate programs. First, create an application that prompts the user for survey responses and outputs each response to a file. Use a Formatter to create a file called numbers.txt. Each integer should be written using method format. Then modify the program in Fig. 7.8 to read...

  • Python programming I need help with the following: Create a class Vehicle. The class should have...

    Python programming I need help with the following: Create a class Vehicle. The class should have name, weight, fuel, number of wheels and move attributes/methods. Create child class that are semi, car, sail boat and bicycle. Give the class the ability to have a constructor that sets the name. Have the other attributes hard coded to suit the vehicle. Give each a way of speaking their attributes as well as the type of animal. For example “Hi I am Lightening...

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