How to resolve the algorithm Power set step by step in the VBScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Power set step by step in the VBScript programming language

Table of Contents

Problem Statement

A   set   is a collection (container) of certain values, without any particular order, and no repeated values. It corresponds with a finite set in mathematics. A set can be implemented as an associative array (partial mapping) in which the value of each key-value pair is ignored. Given a set S, the power set (or powerset) of S, written P(S), or 2S, is the set of all subsets of S.

By using a library or built-in set type, or by defining a set type with necessary operations, write a function with a set S as input that yields the power set 2S of S.

For example, the power set of     {1,2,3,4}     is For a set which contains n elements, the corresponding power set has 2n elements, including the edge cases of empty set. The power set of the empty set is the set which contains itself (20 = 1): And the power set of the set which contains only the empty set, has two subsets, the empty set and the set which contains the empty set (21 = 2):

Extra credit: Demonstrate that your language supports these last two powersets.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Power set step by step in the VBScript programming language

Source code in the vbscript programming language

Function Dec2Bin(n)
	q = n
	Dec2Bin = ""
	Do Until q = 0
		Dec2Bin = CStr(q Mod 2) & Dec2Bin
		q = Int(q / 2)
	Loop
	Dec2Bin = Right("00000" & Dec2Bin,6)
End Function

Function PowerSet(s)
	arrS = Split(s,",")
	PowerSet = "{"
	For i = 0 To 2^(UBound(arrS)+1)-1
		If i = 0 Then
			PowerSet = PowerSet & "{},"
		Else
			binS = Dec2Bin(i)
			PowerSet = PowerSet & "{"
			c = 0
			For j = Len(binS) To 1 Step -1
				If CInt(Mid(binS,j,1)) = 1 Then
					PowerSet = PowerSet & arrS(c) & ","	
				End If
				c = c + 1
			Next
			PowerSet = Mid(PowerSet,1,Len(PowerSet)-1) & "},"
		End If
	Next
	PowerSet = Mid(PowerSet,1,Len(PowerSet)-1) & "}"
End Function

WScript.StdOut.Write PowerSet("1,2,3,4")

  

You may also check:How to resolve the algorithm Find limit of recursion step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Interactive programming (repl) step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the Java programming language
You may also check:How to resolve the algorithm Reduced row echelon form step by step in the Swift programming language
You may also check:How to resolve the algorithm Queue/Definition step by step in the Ring programming language