Generate power set of a given set

Here given code implementation process.

/*
   Java Program 
   Generate power set of a given set
*/
public class PowerSet
{
	public void findPowerSet(char[] text, 
                             String result, int index, int n)
	{
		if (index <= n && n > 0)
		{
			// Display calculated result
			System.out.println("[" + result + "]");
			for (int i = index; i < n; ++i)
			{
				// Find the subsequence using recursively
				findPowerSet(text, result + text[i], i + 1, n);
			}
		}
	}
	public static void main(String[] args)
	{
		PowerSet task = new PowerSet();
		char[] text = {
			'A' , 'B' , 'C' , 'D'
		};
		int n = text.length;
		task.findPowerSet(text, "", 0, n);
	}
}

Output

[]
[A]
[AB]
[ABC]
[ABCD]
[ABD]
[AC]
[ACD]
[AD]
[B]
[BC]
[BCD]
[BD]
[C]
[CD]
[D]
// Include header file
#include <iostream>
using namespace std;
/*
   C++ Program 
   Generate power set of a given set
*/
class PowerSet
{
	public: void findPowerSet(char text[], 
      string result, 
        int index, 
          int n)
	{
		if (index <= n && n > 0)
		{
			// Display calculated result
			cout << "[" << result << "]" << endl;
			for (int i = index; i < n; ++i)
			{
				// Find the subsequence using recursively
				this->findPowerSet(text, 
                                   result  +  text[i], 
                                   i + 1, 
                                   n);
			}
		}
	}
};
int main()
{
	PowerSet *task = new PowerSet();
	char text[] = {
		'A' , 'B' , 'C' , 'D'
	};
	int n = sizeof(text) / sizeof(text[0]);
	task->findPowerSet(text, "", 0, n);
	return 0;
}

Output

[]
[A]
[AB]
[ABC]
[ABCD]
[ABD]
[AC]
[ACD]
[AD]
[B]
[BC]
[BCD]
[BD]
[C]
[CD]
[D]
package main
import "fmt"
/*
   Go Program 
   Generate power set of a given set
*/

func findPowerSet(text[] byte, 
        result string, 
        index int, 
           n int) {
	if index <= n && n > 0 {
		// Display calculated result
		fmt.Println("["+ result+ "]")
		for i := index ; i < n ; i++ {
			// Find the subsequence using recursively
			findPowerSet(text, result + string(text[i]), i + 1, n)
		}
	}
}
func main() {
	var text = [] byte {
		'A',
		'B',
		'C',
		'D',
	}
	var n int = len(text)
	findPowerSet(text, "", 0, n)
}

Output

[]
[A]
[AB]
[ABC]
[ABCD]
[ABD]
[AC]
[ACD]
[AD]
[B]
[BC]
[BCD]
[BD]
[C]
[CD]
[D]
// Include namespace system
using System;
/*
   Csharp Program 
   Generate power set of a given set
*/
public class PowerSet
{
	public void findPowerSet(char[] text, 
      String result, 
      int index, 
      int n)
	{
		if (index <= n && n > 0)
		{
			// Display calculated result
			Console.WriteLine("[" + result + "]");
			for (int i = index; i < n; ++i)
			{
				// Find the subsequence using recursively
				this.findPowerSet(text, 
                                  result + text[i], 
                                  i + 1, 
                                  n);
			}
		}
	}
	public static void Main(String[] args)
	{
		PowerSet task = new PowerSet();
		char[] text = {
			'A' , 'B' , 'C' , 'D'
		};
		int n = text.Length;
		task.findPowerSet(text, "", 0, n);
	}
}

Output

[]
[A]
[AB]
[ABC]
[ABCD]
[ABD]
[AC]
[ACD]
[AD]
[B]
[BC]
[BCD]
[BD]
[C]
[CD]
[D]
<?php
/*
   Php Program 
   Generate power set of a given set
*/
class PowerSet
{
	public	function findPowerSet($text, $result, $index, $n)
	{
		if ($index <= $n && $n > 0)
		{
			// Display calculated result
			print_r("[".$result."]\n");
			for ($i = $index; $i < $n; ++$i)
			{
				// Find the subsequence using recursively
				$this->findPowerSet($text, 
                                    $result.strval($text[$i]), 
                                    $i + 1, $n);
			}
		}
	}
	public static
	function main($args)
	{
		$task = new PowerSet();
		$text = array('A', 'B', 'C', 'D');
		$n = count($text);
		$task->findPowerSet($text, "", 0, $n);
	}
}
PowerSet::main(array());

Output

[]
[A]
[AB]
[ABC]
[ABCD]
[ABD]
[AC]
[ACD]
[AD]
[B]
[BC]
[BCD]
[BD]
[C]
[CD]
[D]
/*
   Node JS Program 
   Generate power set of a given set
*/
class PowerSet
{
	findPowerSet(text, result, index, n)
	{
		if (index <= n && n > 0)
		{
			// Display calculated result
			console.log("[" + result + "]");
			for (var i = index; i < n; ++i)
			{
				// Find the subsequence using recursively
				this.findPowerSet(text, 
                                  result + text[i], 
                                  i + 1, n);
			}
		}
	}
}

function main()
{
	var task = new PowerSet();
	var text = ['A', 'B', 'C', 'D'];
	var n = text.length;
	task.findPowerSet(text, "", 0, n);
}
main();

Output

[]
[A]
[AB]
[ABC]
[ABCD]
[ABD]
[AC]
[ACD]
[AD]
[B]
[BC]
[BCD]
[BD]
[C]
[CD]
[D]
#   Python 3 Program 
#   Generate power set of a given set
class PowerSet :
	def findPowerSet(self, text, result, index, n) :
		if (index <= n and n > 0) :
			#  Display calculated result
			print("[", result ,"]",sep="")
			i = index
			while (i < n) :
				#  Find the subsequence using recursively
				self.findPowerSet(text, 
                                  result + str(text[i]), 
                                  i + 1, n)
				i += 1
			
		
	

def main() :
	task = PowerSet()
	text = ['A', 'B', 'C', 'D']
	n = len(text)
	task.findPowerSet(text, "", 0, n)

if __name__ == "__main__": main()

Output

[]
[A]
[AB]
[ABC]
[ABCD]
[ABD]
[AC]
[ACD]
[AD]
[B]
[BC]
[BCD]
[BD]
[C]
[CD]
[D]
#   Ruby Program 
#   Generate power set of a given set
class PowerSet 
	def findPowerSet(text, result, index, n) 
		if (index <= n && n > 0) 
			#  Display calculated result
			print("[", result ,"]", "\n")
			i = index
			while (i < n) 
				#  Find the subsequence using recursively
				self.findPowerSet(text, 
                                  result + text[i].to_s, 
                                  i + 1, n)
				i += 1
			end

		end

	end

end

def main() 
	task = PowerSet.new()
	text = ['A', 'B', 'C', 'D']
	n = text.length
	task.findPowerSet(text, "", 0, n)
end

main()

Output

[]
[A]
[AB]
[ABC]
[ABCD]
[ABD]
[AC]
[ACD]
[AD]
[B]
[BC]
[BCD]
[BD]
[C]
[CD]
[D]
/*
   Scala Program 
   Generate power set of a given set
*/
class PowerSet()
{
	def findPowerSet(text: Array[Char], 
      result: String, index: Int, n: Int): Unit = {
		if (index <= n && n > 0)
		{
			// Display calculated result
			println("[" + result + "]");
			var i: Int = index;
			while (i < n)
			{
				// Find the subsequence using recursively
				findPowerSet(text, 
                             result + text(i).toString(), i + 1, n);
				i += 1;
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: PowerSet = new PowerSet();
		var text: Array[Char] = Array('A', 'B', 'C', 'D');
		var n: Int = text.length;
		task.findPowerSet(text, "", 0, n);
	}
}

Output

[]
[A]
[AB]
[ABC]
[ABCD]
[ABD]
[AC]
[ACD]
[AD]
[B]
[BC]
[BCD]
[BD]
[C]
[CD]
[D]
import Foundation;
/*
   Swift 4 Program 
   Generate power set of a given set
*/
class PowerSet
{
	func findPowerSet(_ text: [Character], 
  _ result: String, _ index: Int, _ n: Int)
	{
		if (index <= n && n > 0)
		{
			// Display calculated result
			print("[", result ,"]");
			var i: Int = index;
			while (i < n)
			{
				// Find the subsequence using recursively
				self.findPowerSet(text, 
                                  result + String(text[i]), i + 1, n);
				i += 1;
			}
		}
	}
}
func main()
{
	let task: PowerSet = PowerSet();
	let text: [Character] = ["A", "B", "C", "D"];
	let n: Int = text.count;
	task.findPowerSet(text, "", 0, n);
}
main();

Output

[  ]
[ A ]
[ AB ]
[ ABC ]
[ ABCD ]
[ ABD ]
[ AC ]
[ ACD ]
[ AD ]
[ B ]
[ BC ]
[ BCD ]
[ BD ]
[ C ]
[ CD ]
[ D ]
/*
   Kotlin Program 
   Generate power set of a given set
*/
class PowerSet
{
	fun findPowerSet(text: Array < Char > , 
                      result: String, 
                      index: Int, n: Int): Unit
	{
		if (index <= n && n > 0)
		{
			// Display calculated result
			println("[" + result + "]");
			var i: Int = index;
			while (i < n)
			{
				// Find the subsequence using recursively
				this.findPowerSet(text, 
                                  result + text[i].toString(), 
                                  i + 1, n);
				i += 1;
			}
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: PowerSet = PowerSet();
	val text: Array < Char > = arrayOf('A', 'B', 'C', 'D');
	val n: Int = text.count();
	task.findPowerSet(text, "", 0, n);
}

Output

[]
[A]
[AB]
[ABC]
[ABCD]
[ABD]
[AC]
[ACD]
[AD]
[B]
[BC]
[BCD]
[BD]
[C]
[CD]
[D]


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved