Output:
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
printTruthTable("", 0, 3);
$originalArr = array('a', 'b', 'c');
$outputArr = array();
powerSet("", 0, 3);
var_dump($outputArr);
function printTruthTable($str, $start, $strLength) {
if ($start === $strLength) {
if (/*Truth Table*/) {
var_dump($str);
}
if (/*Power Set*/) {
global $originalArr;
global $outputArr;
$tempArr = array();
for($i=0; $i<$strLength; $i++){
if ($str[$i] === 1) {
array_push($tempArr, $originalArr[$i]);
}
}
array_push($outputArr, $tempArr);
}
return;
}
$str[$start] = 0;
printTruthTable($str, $start+1, $strLength);
$str[$start] = 1;
printTruthTable($str, $start+1, $strLength);
}
OR
function printPowerSet($set, $set_size)
{
/*set_size of power set of a set with set_size
n is (2**n -1)*/
$pow_set_size = pow(2, $set_size);
for($counter = 0; $counter < $pow_set_size; $counter++)
{
for($j = 0; $j < $set_size; $j++)
{
if($counter & (1 << $j))
echo $set[$j]; // echo
}
echo PHP_EOL;
}
}
$set = 'abc';
printPowerSet($set, 3);
Showing posts with label Recursion. Show all posts
Showing posts with label Recursion. Show all posts
Sunday, June 9, 2013
Truth Table / Power Set
Tuesday, August 14, 2012
Print all permutations of a string
permute("123", 0, 3);
function permute($str, $start, $strLength) {
if ($start === $strLength) {
var_dump($str);
return;
}
for ($i=$start; $i<$strLength; $i++) {
swap($str, $i, $start);
permute($str, $start+1, $strLength);
swap($str, $start, $i);
}
}
function swap(&$str, $pos1, $pos2) {
$tmp = $str[$pos1];
$str[$pos1] = $str[$pos2];
$str[$pos2] = $tmp;
}
Print all possible combinations of strings that can be made using a keypad given a number
telKeys("2133004655", "2133004655", 0, 10);
$keypad = array("ABC", "DEF", "GHI", "JKL", "MNO", "PQR", "STU", "VWX", "YZ");
function telKeys($original, $new, $start, $strLength) {
global $keypad;
if ($start === $strLength) {
var_dump($new);
return;
}
$new[$start] = $keypad[$original[$start]][0];
telKeys($original, $new, $start+1, $strLength);
$new[$start] = $keypad[($original[$start])][1];
telKeys($original, $new, $start+1, $strLength);
if(($original[$start]) < 8) {
$new[$start] = $keypad[($original[$start])][2];
telKeys($original, $new, $start+1, $strLength);
}
}
Print given string in all combinations of uppercase and lowercasecharacters
Given a string we need to print the string with all possible combinations of the uppercase and lowercase characters in the string.
So given a string "abc", we need to print the following:
ABC
ABc
AbC
Abc
aBC
aBc
abC
abc
ABC
ABc
AbC
Abc
aBC
aBc
abC
abc
Solution is a simple recursive approach where we call the function again and again once with a character in lower case and another time with the same character in upper case. The code is similar to what we would write for all permutations of a string.
lowerUpper("abc", 0, 3);
function lowerUpper($str, $start, $strLength) {
if ($start === $strLength) {
var_dump($str);
return;
}
$str[$start] = strtoupper($str[$start]);
lowerUpper($str, $start+1, $strLength);
$str[$start] = strtolower($str[$start]);
lowerUpper($str, $start+1, $strLength);
}
Subscribe to:
Posts (Atom)