Question

In: Computer Science

Can you write code in Java to find the power set of a given set? For...

Can you write code in Java to find the power set of a given set? For example if S={a,b} the power set is P={{},{a},{b},{a,b}} ( you can also choose any of your favorite programming language). Explaning the code in comment please.

Solutions

Expert Solution

import java.util.*;
public class Main
{
static LinkedList <String> list ;
public static void powerSet(String set[],int setSize)
{
list = new LinkedList<String>();
/*calculating the length of power set,
if set length is n then power set length = 2^n */

int powerSetSize = (int)Math.pow(2,setSize);
int counter,i;
//run counter from 000 to 111
for(counter = 0; counter < powerSetSize; counter++)
{ String powerSetElement ="";
for(i = 0; i < setSize; i++)
{
/*check if ith bit in the counter is set,
then add element from set to powerSetElement */
if((counter & (1 << i)) > 0)
{
powerSetElement = powerSetElement+set[i];
}
}
// add powerSetElement element into list,
list.add(powerSetElement);
}
}
   public static void main(String[] args)
   {
   //set element
       String set[] = {"a","b","c"};
       // powerSet method that generate a power set element into list,
       powerSet(set,3);
       /*printin all power set element into power set formate,
       ex - : {{},{a},{b},{ab}}*/

       System.out.print("{");
       for(int i = 0; i < list.size()-1; i++ )
       {
       System.out.print("{"+list.get(i)+"},");
       }
       System.out.print("{"+list.get(list.size()-1)+"}"+"}");
   }
}


Related Solutions

In Java, a set of integers is given. write a function to find 2 integers in...
In Java, a set of integers is given. write a function to find 2 integers in this set that sums upto a target value. i.e [1,5,2,0,11,3] target = 7 result [5,2] i.e [1,5,4,0,14,-7] target = 9 result [5,4] NOTE: THE SAME INTEGER CANNOT BE USED TWICE !!!
Parse string java code Write a recursive program that can calculate the value of a given...
Parse string java code Write a recursive program that can calculate the value of a given polynomial in a string, which is not more than the tenth order, for the given x. The polynomial will be given in the following format and should display the value of the polynomial for spaced-out x Using index of for -/+
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure...
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure out a number chosen by a human user. The human user will think of a number between 1 and 100. The program will make guesses and the user will tell the program to guess higher or lower.                                                                   Requirements The purpose of the assignment is to practice writing functions. Although it would be possible to write the entire program in the main function, your...
Write a Java program where you will set the datatype and variables to find the value...
Write a Java program where you will set the datatype and variables to find the value of following expression. The Values of the variables to be used are given below a) 101 + 0) / 3 b) 3.0e-6 * 10000000.1 c) true && true d) false && true e) (false && false) || (true && true) f) (false || false) && (true && true)
Language for this question is Java write the code for the given assignment Given an n...
Language for this question is Java write the code for the given assignment Given an n x n matrix, where every row and column is sorted in non-decreasing order. Print all elements of matrix in sorted order.Input: The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the matrix. Then the next line contains the n x n elements...
Can you write code or class in java accepting from the user a + b as...
Can you write code or class in java accepting from the user a + b as a string and accepting their value from the user and the out but will be the sum of the variables.
Java programming Write the max-heapify code and test it and then write the program to find...
Java programming Write the max-heapify code and test it and then write the program to find the three largest values of the array without sorting the entire array to get values.
write this java code: One statistical operation that is sometimes performed on a set of data...
write this java code: One statistical operation that is sometimes performed on a set of data values is to remove values that are far from the average. Write a program that reads real values from a text file, one per line. Store the data values as Double objects in an instance of the class java.util.ArrayList. Then Use an iterator to compute the average and standard deviation of the values. Display these results. Use a second iterator to remove any value...
CODE IN JAVA** I(a). Given a pointer to the root of a binary tree write a...
CODE IN JAVA** I(a). Given a pointer to the root of a binary tree write a routine that will mark (use a negative number like -999 for the info field) every node in the tree that currently has only a left son. You can assume the root of the tree has both a right and left son. When finished tell me how many nodes had only a left son as well as how many nodes are in the tree in...
Write a program to multiply a polynomial with a given number. Code needed in java.
Write a program to multiply a polynomial with a given number. Code needed in java.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT