Posted on by Kalkicode
Code String

# Display all True Boolean expression

This is an simple problem of mathematics. Our here given of some operands and operators, When combining operands and operators then this is produce an boolean value. Either true(1) or false(0) value. Our goal is to display possible combinations which is produce true (1) value.

Note that each expression will be contain all operators and operands. Here operand position are predefined but can be modified operator position. For example.

``````Example 1
char operand[] = "1101";
char operators[] = "|&^";

Output
((( 1 | 1 ) & 0 ) ^ 1 )
((( 1 | 1 ) ^ 0 ) & 1 )
((( 1 & 1 ) ^ 0 ) | 1 )
((( 1 ^ 1 ) & 0 ) | 1 )

Example 2

char operand[] = "1001";
char operators[] = "|&^";

((( 1 | 0 ) & 0 ) ^ 1 )
((( 1 | 0 ) ^ 0 ) & 1 )
((( 1 & 0 ) | 0 ) ^ 1 )
((( 1 & 0 ) ^ 0 ) | 1 )
((( 1 ^ 0 ) & 0 ) | 1 )
((( 1 ^ 0 ) | 0 ) & 1 )

Example 3
char operand[] = "101";
char operators[] = "|&";
(( 1 | 0 ) & 1 )
(( 1 & 0 ) | 1 )

Example 4

char operand[] = "01010";
char operators[] = "|&^>";
Output
(((( 0 | 1 ) & 0 ) ^ 1 ) > 0 )
(((( 0 | 1 ) ^ 0 ) & 1 ) > 0 )
(((( 0 | 1 ) > 0 ) & 1 ) ^ 0 )
(((( 0 & 1 ) | 0 ) ^ 1 ) > 0 )
(((( 0 & 1 ) ^ 0 ) | 1 ) > 0 )
(((( 0 & 1 ) > 0 ) ^ 1 ) | 0 )
(((( 0 & 1 ) > 0 ) | 1 ) ^ 0 )
(((( 0 ^ 1 ) & 0 ) | 1 ) > 0 )
(((( 0 ^ 1 ) | 0 ) & 1 ) > 0 )
(((( 0 ^ 1 ) > 0 ) & 1 ) | 0 )
(((( 0 > 1 ) & 0 ) ^ 1 ) | 0 )
(((( 0 > 1 ) & 0 ) | 1 ) ^ 0 )

Example 5

char operand[] = "10100";
char operators[] = "|&^>";
Output
(((( 1 | 0 ) & 1 ) ^ 0 ) > 0 )
(((( 1 | 0 ) & 1 ) > 0 ) ^ 0 )
(((( 1 & 0 ) | 1 ) ^ 0 ) > 0 )
(((( 1 & 0 ) | 1 ) > 0 ) ^ 0 )
(((( 1 & 0 ) ^ 1 ) | 0 ) > 0 )
(((( 1 & 0 ) ^ 1 ) > 0 ) | 0 )
(((( 1 ^ 0 ) & 1 ) | 0 ) > 0 )
(((( 1 ^ 0 ) & 1 ) > 0 ) | 0 )
(((( 1 > 0 ) & 1 ) ^ 0 ) | 0 )
(((( 1 > 0 ) & 1 ) | 0 ) ^ 0 )

``````

Note that parenthesis is indicates execution of expression. You can write same expression in different ways. For example

``````0 | 1 & 0 ^ 1 > 0

(((( 0 | 1 ) & 0 ) ^ 1 ) > 0 ) = true
or
(((0 | 1 ) & 0 )  ^ ( 1  > 0 )) = true
or
(((0 | 1 ) & (( 0 ^ 1 ) > 0 ))) = true
or
(((0 | ((1  & 0 ) ^ ( 1  > 0 )))) = true
//more ...``````

In this situation pick any one result. Before solve this problem note that size of operand - 1 operator will used in given inputs. Mainly {^,>,<,|,&} operator are using in expression.

Here given code implementation process.

``````// Java program
// Display all True Boolean expression
public class BooleanExpression
{
private String operand;
private String operators;
BooleanExpression(String operand, String operators)
{
this.operators = operators;
this.operand = operand;
}
public boolean operate(int a, char op, int b)
{

// Assume (&^|><) operator are used
switch (op)
{
case '&':
return (a & b) == 1;
case '|':
return (a | b) == 1;
case '^':
return (a ^ b) == 1;
case '>':
return a > b;
case '<':
return a < b;
}
// invalid operator
return false;
}
public void brackets(int size)
{
for (int i = 0; i <= size; ++i)
{
System.out.print("(");
}
}
// Swapping two string elements by index
public void swap(int start, int end)
{
char a = operators.charAt(start);
char b = operators.charAt(end);
operators = operators.substring(0, end) + a + operators.substring(end + 1);
operators = operators.substring(0, start) + b + operators.substring(start + 1);
}
// Find and Display true boolean expression
public void expression(int k)
{
if (k > operators.length())
{
return;
}
if (k == operators.length() - 1)
{
int size = 0;
int b = 0, a = 0;
boolean result = false;
for (int i = 1; i < operand.length(); ++i)
{
if (i == 1)
{
if (operand.charAt(0) == '1')
{
a = 1;
}
if (operand.charAt(i) == '1')
{
b = 1;
}
// Base case
result = operate(a, operators.charAt(size), b);
size++;
}
else
{
if (operand.charAt(i) == '1')
{
b = 1;
}
if (result == true)
{
a = 1;
}
result = operate(a, operators.charAt(size), b);
size++;
}
b = 0;
a = 0;
}
if (result == true)
{
// Display valid expression
size = 0;
brackets(operand.length() - 2);
for (int i = 1; i < operand.length(); ++i)
{
if (i == 1)
{
System.out.print(" " + operand.charAt(0) + " " + operators.charAt(size) + " " + operand.charAt(i) + " )");
size++;
}
else
{
System.out.print(" " + operators.charAt(size) + " " + operand.charAt(i) + " )");
size++;
}
}
System.out.println();
}
}
for (int i = k; i < operators.length(); ++i)
{
swap(k, i);
// recursive call
expression(k + 1);
// back to previous operator position
swap(k, i);
}
}
public static void main(String[] args)
{
// Create instance
BooleanExpression task = new BooleanExpression("01010", "|&^>");
}
}``````

#### Output

``````(((( 0 | 1 ) & 0 ) ^ 1 ) > 0 )
(((( 0 | 1 ) ^ 0 ) & 1 ) > 0 )
(((( 0 | 1 ) > 0 ) & 1 ) ^ 0 )
(((( 0 & 1 ) | 0 ) ^ 1 ) > 0 )
(((( 0 & 1 ) ^ 0 ) | 1 ) > 0 )
(((( 0 & 1 ) > 0 ) ^ 1 ) | 0 )
(((( 0 & 1 ) > 0 ) | 1 ) ^ 0 )
(((( 0 ^ 1 ) & 0 ) | 1 ) > 0 )
(((( 0 ^ 1 ) | 0 ) & 1 ) > 0 )
(((( 0 ^ 1 ) > 0 ) & 1 ) | 0 )
(((( 0 > 1 ) & 0 ) ^ 1 ) | 0 )
(((( 0 > 1 ) & 0 ) | 1 ) ^ 0 )``````
``````// C Program
// Display all True Boolean expression
#include <stdio.h>
// Swap element in array
void swap(char *str, int i, int j)
{
char temp = str[i];
str[i] = str[j];
str[j] = temp;
}
// This method is execute an expression
// Using two operands and one operator and return its result
int operate(int a, char op, int b)
{
// Assume (&^|><) operator are used
switch (op)
{
case '&':
return a & b;
case '|':
return a | b;
case '^':
return a ^ b;
case '>':
return a > b;
case '<':
return a < b;
}
// invalid operator
return -1;
}
void brackets(int size)
{
for (int i = 0; i <= size; ++i)
{
printf("(");
}
}
// Find and Display true boolean expression
void expression(char operand[],
char operators[], int s1, int s2, int k)
{
if (k > s2)
{
return;
}
if (k == s2)
{
int size = 0;
int result = 0;
for (int i = 1; i < s1 - 1; ++i)
{
if (i == 1)
{
// Base case
result = operate(operand[0] - '0',
operators[size], operand[i] - '0');
size++;
}
else
{
result = operate(result,
operators[size], operand[i] - '0');
size++;
}
}
if (result == 1)
{
// Display valid expression
size = 0;
brackets(s1 - 3);
for (int i = 1; i < s1 - 1; ++i)
{
if (i == 1)
{
printf(" %c %c %c )",
operand[0], operators[size], operand[i]);
size++;
}
else
{
printf(" %c %c )",
operators[size], operand[i]);
size++;
}
}
printf("\n");
}
}
for (int i = k; i <= s2; ++i)
{
swap(operators, k, i);
//recursive call
expression(operand, operators, s1, s2, k + 1);
//back to previous operator position
swap(operators, k, i);
}
}
int main()
{
// operands
char operand[] = "01010";
// Get the length of operands
int length = sizeof(operand) / sizeof(operand[0]);
// operators
char operators[] = "|&^>";
// Get the length of operators
int size = sizeof(operators) / sizeof(operators[0]);
// Test
expression(operand, operators, length, size - 2, 0);
return 0;
}``````

#### Output

``````(((( 0 | 1 ) & 0 ) ^ 1 ) > 0 )
(((( 0 | 1 ) ^ 0 ) & 1 ) > 0 )
(((( 0 | 1 ) > 0 ) & 1 ) ^ 0 )
(((( 0 & 1 ) | 0 ) ^ 1 ) > 0 )
(((( 0 & 1 ) ^ 0 ) | 1 ) > 0 )
(((( 0 & 1 ) > 0 ) ^ 1 ) | 0 )
(((( 0 & 1 ) > 0 ) | 1 ) ^ 0 )
(((( 0 ^ 1 ) & 0 ) | 1 ) > 0 )
(((( 0 ^ 1 ) | 0 ) & 1 ) > 0 )
(((( 0 ^ 1 ) > 0 ) & 1 ) | 0 )
(((( 0 > 1 ) & 0 ) ^ 1 ) | 0 )
(((( 0 > 1 ) & 0 ) | 1 ) ^ 0 )``````
``````// Include header file
#include <iostream>
#include <string>

using namespace std;
// C++ program
// Display all True Boolean expression
class BooleanExpression
{
public:
string operand;
string operators;
BooleanExpression(string operand, string operators)
{
this->operators = operators;
this->operand = operand;
}
bool operate(int a, char op, int b)
{
bool a1 = true;
bool b1 = true;
// Converted into boolean
if (a == 0)
{
a1 = false;
}
if (b == 0)
{
b1 = false;
}
// Assume (&^|><) operator are used
switch (op)
{
case '&':
return a1 &b1;
case '|':
return a1 | b1;
case '^':
return a1 ^ b1;
case '>':
return a > b;
case '<':
return a < b;
}
// invalid operator
return false;
}
void brackets(int size)
{
for (int i = 0; i <= size; ++i)
{
cout << "(";
}
}
// Swapping two string elements by index
void swap(int start, int end)
{
char temp = operators[start];
operators[start] = operators[end];
operators[end] = temp;
}
// Find and Display true boolean expression
void expression(int k)
{
if (k > this->operators.length())
{
return;
}
if (k == this->operators.length() - 1)
{
int size = 0;
int b = 0;
int a = 0;
bool result = false;
for (int i = 1; i < this->operand.length(); ++i)
{
if (i == 1)
{
if (this->operand[0] == '1')
{
a = 1;
}
if (this->operand[i] == '1')
{
b = 1;
}
// Base case
result = this->operate(a, this->operators[size], b);
size++;
}
else
{
if (this->operand[i] == '1')
{
b = 1;
}
if (result == true)
{
a = 1;
}
result = this->operate(a, this->operators[size], b);
size++;
}
b = 0;
a = 0;
}
if (result == true)
{
// Display valid expression
size = 0;
this->brackets(this->operand.length() - 2);
for (int i = 1; i < this->operand.length(); ++i)
{
if (i == 1)
{
cout << " " << this->operand[0] << " " << this->operators[size] << " " << this->operand[i] << " )";
size++;
}
else
{
cout << " " << this->operators[size] << " " << this->operand[i] << " )";
size++;
}
}
cout << endl;
}
}
for (int i = k; i < this->operators.length(); ++i)
{
this->swap(k, i);
// recursive call
this->expression(k + 1);
// back to previous operator position
this->swap(k, i);
}
}
};
int main()
{
// Create instance
BooleanExpression *task = new BooleanExpression("01010", "|&^>");
return 0;
}``````

#### Output

``````(((( 0 | 1 ) & 0 ) ^ 1 ) > 0 )
(((( 0 | 1 ) ^ 0 ) & 1 ) > 0 )
(((( 0 | 1 ) > 0 ) & 1 ) ^ 0 )
(((( 0 & 1 ) | 0 ) ^ 1 ) > 0 )
(((( 0 & 1 ) ^ 0 ) | 1 ) > 0 )
(((( 0 & 1 ) > 0 ) ^ 1 ) | 0 )
(((( 0 & 1 ) > 0 ) | 1 ) ^ 0 )
(((( 0 ^ 1 ) & 0 ) | 1 ) > 0 )
(((( 0 ^ 1 ) | 0 ) & 1 ) > 0 )
(((( 0 ^ 1 ) > 0 ) & 1 ) | 0 )
(((( 0 > 1 ) & 0 ) ^ 1 ) | 0 )
(((( 0 > 1 ) & 0 ) | 1 ) ^ 0 )``````
``````package main
import "fmt"
// Go program
// Display all True Boolean expression
type BooleanExpression struct {
operand string
operators string
}
func getBooleanExpression(operand string, operators string) * BooleanExpression {
var me *BooleanExpression = &BooleanExpression {}
me.operators = operators
me.operand = operand
return me
}
func(this BooleanExpression) operate(a int, op byte, b int) bool {

// Assume (&^|><) operator are used
switch (op) {
case '&':
return a & b == 1
case '|':
return a | b == 1
case '^':
return a ^ b == 1
case '>':
return a > b
case '<':
return a < b
}
// invalid operator
return false
}
func(this BooleanExpression) brackets(size int) {
for i := 0 ; i <= size ; i++ {
fmt.Print("(")
}
}
// Swapping two string elements by index
func(this *BooleanExpression) swap(start, end int) {
r := []rune(this.operators)
r[start],r[end] = r[end],r[start]
this.operators = string(r)
}
// Find and Display true boolean expression
func(this BooleanExpression) expression(k int) {
if k > len(this.operators) {
return
}
if k == len(this.operators) - 1 {
var size int = 0
var b int = 0
var a int = 0
var result bool = false
for i := 1 ; i < len(this.operand) ; i++ {
if i == 1 {
if this.operand[0] == '1' {
a = 1
}
if this.operand[i] == '1' {
b = 1
}
// Base case
result = this.operate(a, this.operators[size], b)
size++
} else {
if this.operand[i] == '1' {
b = 1
}
if result == true {
a = 1
}
result = this.operate(a, this.operators[size], b)
size++
}
b = 0
a = 0
}
if result == true {
// Display valid expression
size = 0
this.brackets(len(this.operand) - 2)
for i := 1 ; i < len(this.operand) ; i++ {
if i == 1 {
fmt.Print(" ", string(this.operand[0]), " ",
string(this.operators[size]),
" ", string(this.operand[i]), " )")
size++
} else {
fmt.Print(" ", string(this.operators[size]), " ",
string(this.operand[i]), " )")
size++
}
}
fmt.Println()
}
}
for i := k ; i < len(this.operators) ; i++ {
this.swap(k, i)
// recursive call
this.expression(k + 1)
// back to previous operator position
this.swap(k, i)
}
}
func main() {
// Create instance
var task * BooleanExpression = getBooleanExpression("01010", "|&^>")
}``````

#### Output

``````(((( 0 | 1 ) & 0 ) ^ 1 ) > 0 )
(((( 0 | 1 ) ^ 0 ) & 1 ) > 0 )
(((( 0 | 1 ) > 0 ) & 1 ) ^ 0 )
(((( 0 & 1 ) | 0 ) ^ 1 ) > 0 )
(((( 0 & 1 ) ^ 0 ) | 1 ) > 0 )
(((( 0 & 1 ) > 0 ) ^ 1 ) | 0 )
(((( 0 & 1 ) > 0 ) | 1 ) ^ 0 )
(((( 0 ^ 1 ) & 0 ) | 1 ) > 0 )
(((( 0 ^ 1 ) | 0 ) & 1 ) > 0 )
(((( 0 ^ 1 ) > 0 ) & 1 ) | 0 )
(((( 0 > 1 ) & 0 ) ^ 1 ) | 0 )
(((( 0 > 1 ) & 0 ) | 1 ) ^ 0 )``````
``````// Include namespace system
using System;
// Csharp program
// Display all True Boolean expression
public class BooleanExpression
{
private String operand;
private String operators;
BooleanExpression(String operand, String operators)
{
this.operators = operators;
this.operand = operand;
}
public Boolean operate(int a, char op, int b)
{
// Assume (&^|><) operator are used
switch (op)
{
case '&':
return (a & b) == 1;
case '|':
return (a | b) == 1;
case '^':
return (a ^ b) == 1;
case '>':
return a > b;
case '<':
return a < b;
}
// invalid operator
return false;
}
public void brackets(int size)
{
for (int i = 0; i <= size; ++i)
{
Console.Write("(");
}
}
// Swapping two string elements by index
public void swap(int start, int end)
{
if (start > operators.Length ||
end > operators.Length ||
start == end)
{
return;
}
char a = operators[start];
char[] ch = operators.ToCharArray();
ch[start] = operators[end];
ch[end] = a;
operators = new string(ch);
}
// Find and Display true Boolean expression
public void expression(int k)
{
if (k > this.operators.Length)
{
return;
}
if (k == this.operators.Length - 1)
{
int size = 0;
int b = 0;
int a = 0;
Boolean result = false;
for (int i = 1; i < this.operand.Length; ++i)
{
if (i == 1)
{
if (this.operand[0] == '1')
{
a = 1;
}
if (this.operand[i] == '1')
{
b = 1;
}
// Base case
result = this.operate(a, this.operators[size], b);
size++;
}
else
{
if (this.operand[i] == '1')
{
b = 1;
}
if (result == true)
{
a = 1;
}
result = this.operate(a, this.operators[size], b);
size++;
}
b = 0;
a = 0;
}
if (result == true)
{
// Display valid expression
size = 0;
this.brackets(this.operand.Length - 2);
for (int i = 1; i < this.operand.Length; ++i)
{
if (i == 1)
{
Console.Write(" " + this.operand[0] + " " +
this.operators[size] + " " +
this.operand[i] + " )");
size++;
}
else
{
Console.Write(" " + this.operators[size] + " " +
this.operand[i] + " )");
size++;
}
}
Console.WriteLine();
}
}
for (int i = k; i < this.operators.Length; ++i)
{
this.swap(k, i);
// recursive call
this.expression(k + 1);
// back to previous operator position
this.swap(k, i);
}
}
public static void Main(String[] args)
{
// Create instance
BooleanExpression task = new BooleanExpression("01010", "|&^>");
}
}``````

#### Output

``````(((( 0 | 1 ) & 0 ) ^ 1 ) > 0 )
(((( 0 | 1 ) ^ 0 ) & 1 ) > 0 )
(((( 0 | 1 ) > 0 ) & 1 ) ^ 0 )
(((( 0 & 1 ) | 0 ) ^ 1 ) > 0 )
(((( 0 & 1 ) ^ 0 ) | 1 ) > 0 )
(((( 0 & 1 ) > 0 ) ^ 1 ) | 0 )
(((( 0 & 1 ) > 0 ) | 1 ) ^ 0 )
(((( 0 ^ 1 ) & 0 ) | 1 ) > 0 )
(((( 0 ^ 1 ) | 0 ) & 1 ) > 0 )
(((( 0 ^ 1 ) > 0 ) & 1 ) | 0 )
(((( 0 > 1 ) & 0 ) ^ 1 ) | 0 )
(((( 0 > 1 ) & 0 ) | 1 ) ^ 0 )``````
``````<?php
// Php program
// Display all True Boolean expression
class BooleanExpression
{
private \$operand;
private \$operators;

function __construct(\$operand, \$operators)
{
\$this->operators = \$operators;
\$this->operand = \$operand;
}
public  function operate(\$a, \$op, \$b)
{
// Assume (&^|><) operator are used
switch (\$op)
{
case '&':
return (\$a & \$b) == 1;
case '|':
return (\$a | \$b) == 1;
case '^':
return (\$a ^ \$b) == 1;
case '>':
return \$a > \$b;
case '<':
return \$a < \$b;
}
// invalid operator
return false;
}
public  function brackets(\$size)
{
for (\$i = 0; \$i <= \$size; ++\$i)
{
print_r("(");
}
}
// Swapping two string elements by index
public  function swap(\$start, \$end)
{
\$a = \$this->operators[\$start];
\$b = \$this->operators[\$end];
\$this->operators = substr(\$this->operators, 0,
\$end - 0).strval(\$a).substr(\$this->operators,
\$end + 1);
\$this->operators = substr(\$this->operators,
0, \$start - 0).strval(\$b).substr(\$this->operators,
\$start + 1);
}
// Find and Display true boolean expression
public  function expression(\$k)
{
if (\$k > strlen(\$this->operators))
{
return;
}
if (\$k == strlen(\$this->operators) - 1)
{
\$size = 0;
\$b = 0;
\$a = 0;
\$result = false;
for (\$i = 1; \$i < strlen(\$this->operand); ++\$i)
{
if (\$i == 1)
{
if (\$this->operand[0] == '1')
{
\$a = 1;
}
if (\$this->operand[\$i] == '1')
{
\$b = 1;
}
// Base case
\$result = \$this->operate(\$a, \$this->operators[\$size], \$b);
\$size++;
}
else
{
if (\$this->operand[\$i] == '1')
{
\$b = 1;
}
if (\$result == true)
{
\$a = 1;
}
\$result = \$this->operate(\$a, \$this->operators[\$size], \$b);
\$size++;
}
\$b = 0;
\$a = 0;
}
if (\$result == true)
{
// Display valid expression
\$size = 0;
\$this->brackets(strlen(\$this->operand) - 2);
for (\$i = 1; \$i < strlen(\$this->operand); ++\$i)
{
if (\$i == 1)
{
print_r(" ".strval(\$this->operand[0]).
" ".strval(\$this->operators[\$size]).
" ".strval(\$this->operand[\$i]).
" )");
\$size++;
}
else
{
print_r(" ".strval(\$this->operators[\$size]).
" ".strval(\$this->operand[\$i]).
" )");
\$size++;
}
}
print_r("\n");
}
}
for (\$i = \$k; \$i < strlen(\$this->operators); ++\$i)
{
\$this->swap(\$k, \$i);
// recursive call
\$this->expression(\$k + 1);
// back to previous operator position
\$this->swap(\$k, \$i);
}
}
public static
function main(\$args)
{
// Create instance
}
}
BooleanExpression::main(array());``````

#### Output

``````(((( 0 | 1 ) & 0 ) ^ 1 ) > 0 )
(((( 0 | 1 ) ^ 0 ) & 1 ) > 0 )
(((( 0 | 1 ) > 0 ) & 1 ) ^ 0 )
(((( 0 & 1 ) | 0 ) ^ 1 ) > 0 )
(((( 0 & 1 ) ^ 0 ) | 1 ) > 0 )
(((( 0 & 1 ) > 0 ) ^ 1 ) | 0 )
(((( 0 & 1 ) > 0 ) | 1 ) ^ 0 )
(((( 0 ^ 1 ) & 0 ) | 1 ) > 0 )
(((( 0 ^ 1 ) | 0 ) & 1 ) > 0 )
(((( 0 ^ 1 ) > 0 ) & 1 ) | 0 )
(((( 0 > 1 ) & 0 ) ^ 1 ) | 0 )
(((( 0 > 1 ) & 0 ) | 1 ) ^ 0 )``````
``````// Node JS program
// Display all True Boolean expression
class BooleanExpression
{
constructor(operand, operators)
{
this.operators = operators;
this.operand = operand;
}
operate(a, op, b)
{
// Assume (&^|><) operator are used
switch (op)
{
case '&':
return (a & b) == 1;
case '|':
return (a | b) == 1;
case '^':
return (a ^ b) == 1;
case '>':
return a > b;
case '<':
return a < b;
}
// invalid operator
return false;
}
brackets(size)
{
for (var i = 0; i <= size; ++i)
{
process.stdout.write("(");
}
}
// Swapping two string elements by index
swap(start, end)
{
var a = this.operators.charAt(start);
var b = this.operators.charAt(end);
this.operators = this.operators.substring(0, end) + a +
this.operators.substring(end + 1);
this.operators = this.operators.substring(0, start) + b +
this.operators.substring(start + 1);
}
// Find and Display true boolean expression
expression(k)
{
if (k > this.operators.length)
{
return;
}
if (k == this.operators.length - 1)
{
var size = 0;
var b = 0;
var a = 0;
var result = false;
for (var i = 1; i < this.operand.length; ++i)
{
if (i == 1)
{
if (this.operand.charAt(0) == '1')
{
a = 1;
}
if (this.operand.charAt(i) == '1')
{
b = 1;
}
// Base case
result = this.operate(a, this.operators.charAt(size), b);
size++;
}
else
{
if (this.operand.charAt(i) == '1')
{
b = 1;
}
if (result == true)
{
a = 1;
}
result = this.operate(a, this.operators.charAt(size), b);
size++;
}
b = 0;
a = 0;
}
if (result == true)
{
// Display valid expression
size = 0;
this.brackets(this.operand.length - 2);
for (var i = 1; i < this.operand.length; ++i)
{
if (i == 1)
{
process.stdout.write(" " +
this.operand.charAt(0) + " " +
this.operators.charAt(size) + " " +
this.operand.charAt(i) + " )");
size++;
}
else
{
process.stdout.write(" " +
this.operators.charAt(size) + " " +
this.operand.charAt(i) + " )");
size++;
}
}
console.log();
}
}
for (var i = k; i < this.operators.length; ++i)
{
this.swap(k, i);
// recursive call
this.expression(k + 1);
// back to previous operator position
this.swap(k, i);
}
}
}

function main()
{
// Create instance
var task = new BooleanExpression("01010", "|&^>");
}
main();``````

#### Output

``````(((( 0 | 1 ) & 0 ) ^ 1 ) > 0 )
(((( 0 | 1 ) ^ 0 ) & 1 ) > 0 )
(((( 0 | 1 ) > 0 ) & 1 ) ^ 0 )
(((( 0 & 1 ) | 0 ) ^ 1 ) > 0 )
(((( 0 & 1 ) ^ 0 ) | 1 ) > 0 )
(((( 0 & 1 ) > 0 ) ^ 1 ) | 0 )
(((( 0 & 1 ) > 0 ) | 1 ) ^ 0 )
(((( 0 ^ 1 ) & 0 ) | 1 ) > 0 )
(((( 0 ^ 1 ) | 0 ) & 1 ) > 0 )
(((( 0 ^ 1 ) > 0 ) & 1 ) | 0 )
(((( 0 > 1 ) & 0 ) ^ 1 ) | 0 )
(((( 0 > 1 ) & 0 ) | 1 ) ^ 0 )``````
``````#  Python 3 program
#  Display all True Boolean expression
class BooleanExpression :
def __init__(self, operand, operators) :
self.operators = operators
self.operand = operand

def operate(self, a, op, b) :
if (op == '&') :
return (a & b) == 1

elif(op == '|') :
return (a | b) == 1

elif(op == '^') :
return (a ^ b) == 1

elif(op == '>') :
return a > b

elif(op == '<') :
return a < b

#  invalid operator
return False

def brackets(self, size) :
i = 0
while (i <= size) :
print("(", end = "")
i += 1

#  Swapping two string elements by index
def swap(self, start, end) :
a = self.operators[start]
b = self.operators[end]
self.operators = self.operators[0: end] + str(a) + self.operators[end + 1: ]
self.operators = self.operators[0: start] + str(b) + self.operators[start + 1: ]

#  Find and Display true boolean expression
def expression(self, k) :
if (k > len(self.operators)) :
return

if (k == len(self.operators) - 1) :
size = 0
b = 0
a = 0
result = False
i = 1
while (i < len(self.operand)) :
if (i == 1) :
if (self.operand[0] == '1') :
a = 1

if (self.operand[i] == '1') :
b = 1

#  Base case
result = self.operate(a, self.operators[size], b)
size += 1
else :
if (self.operand[i] == '1') :
b = 1

if (result == True) :
a = 1

result = self.operate(a, self.operators[size], b)
size += 1

b = 0
a = 0
i += 1

if (result == True) :
#  Display valid expression
size = 0
self.brackets(len(self.operand) - 2)
i = 1
while (i < len(self.operand)) :
if (i == 1) :
print("", self.operand[0] ,"",
self.operators[size] ,"",
self.operand[i] ,")", end = "")
size += 1
else :
print(" ", self.operators[size] ,
"", self.operand[i] ,")", end = "")
size += 1

i += 1

print()

i = k
while (i < len(self.operators)) :
self.swap(k, i)
#  recursive call
self.expression(k + 1)
#  back to previous operator position
self.swap(k, i)
i += 1

def main() :
#  Create instance

if __name__ == "__main__": main()``````

#### Output

``````(((( 0  |  1 )  &  0 )  ^  1 )  >  0 )
(((( 0  |  1 )  ^  0 )  &  1 )  >  0 )
(((( 0  |  1 )  >  0 )  &  1 )  ^  0 )
(((( 0  &  1 )  |  0 )  ^  1 )  >  0 )
(((( 0  &  1 )  ^  0 )  |  1 )  >  0 )
(((( 0  &  1 )  >  0 )  ^  1 )  |  0 )
(((( 0  &  1 )  >  0 )  |  1 )  ^  0 )
(((( 0  ^  1 )  &  0 )  |  1 )  >  0 )
(((( 0  ^  1 )  |  0 )  &  1 )  >  0 )
(((( 0  ^  1 )  >  0 )  &  1 )  |  0 )
(((( 0  >  1 )  &  0 )  ^  1 )  |  0 )
(((( 0  >  1 )  &  0 )  |  1 )  ^  0 )``````
``````#  Ruby program
#  Display all True Boolean expression
class BooleanExpression
# Define the accessor and reader of class BooleanExpression
attr_accessor :operand, :operators
def initialize(operand, operators)
self.operators = operators
self.operand = operand
end

def operate(a, op, b)
#  Assume (&^|><) operator are used
case (op)
when '&'
return (a & b) == 1
when '|'
return (a | b) == 1
when '^'
return (a ^ b) == 1
when '>'
return a > b
when '<'
return a < b
end

#  invalid operator
return false
end

def brackets(size)
i = 0
while (i <= size)
print("(")
i += 1
end

end

#  Swapping two string elements by index
def swap(start, ends)
a = self.operators[start]
b = self.operators[ends]
self.operators[start],self.operators[ends] =
self.operators[ends],self.operators[start]
end

#  Find and Display true boolean expression
def expression(k)
if (k > self.operators.length)
return
end

if (k == self.operators.length - 1)
size = 0
b = 0
a = 0
result = false
i = 1
while (i < self.operand.length)
if (i == 1)
if (self.operand[0] == '1')
a = 1
end

if (self.operand[i] == '1')
b = 1
end

#  Base case
result = self.operate(a, self.operators[size], b)
size += 1
else

if (self.operand[i] == '1')
b = 1
end

if (result == true)
a = 1
end

result = self.operate(a, self.operators[size], b)
size += 1
end

b = 0
a = 0
i += 1
end

if (result == true)
#  Display valid expression
size = 0
self.brackets(self.operand.length - 2)
i = 1
while (i < self.operand.length)
if (i == 1)
print(" ", self.operand[0] ," ",
self.operators[size] ," ",
self.operand[i] ," )")
size += 1
else

print(" ", self.operators[size] ," ",
self.operand[i] ," )")
size += 1
end

i += 1
end

print("\n")
end

end

i = k
while (i < self.operators.length)
self.swap(k, i)
#  recursive call
self.expression(k + 1)
#  back to previous operator position
self.swap(k, i)
i += 1
end

end

end

def main()
#  Create instance
end

main()``````

#### Output

``````(((( 0 | 1 ) & 0 ) ^ 1 ) > 0 )
(((( 0 | 1 ) ^ 0 ) & 1 ) > 0 )
(((( 0 | 1 ) > 0 ) & 1 ) ^ 0 )
(((( 0 & 1 ) | 0 ) ^ 1 ) > 0 )
(((( 0 & 1 ) ^ 0 ) | 1 ) > 0 )
(((( 0 & 1 ) > 0 ) ^ 1 ) | 0 )
(((( 0 & 1 ) > 0 ) | 1 ) ^ 0 )
(((( 0 ^ 1 ) & 0 ) | 1 ) > 0 )
(((( 0 ^ 1 ) | 0 ) & 1 ) > 0 )
(((( 0 ^ 1 ) > 0 ) & 1 ) | 0 )
(((( 0 > 1 ) & 0 ) ^ 1 ) | 0 )
(((( 0 > 1 ) & 0 ) | 1 ) ^ 0 )
``````
``````import scala.collection.mutable._;
// Scala program
// Display all True Boolean expression
class BooleanExpression(var operand: String,
var operators: String)
{

def operate(a: Int, op: Char, b: Int): Boolean = {
// Assume (&^|><) operator are used
op match
{
case '&' => return (a & b) == 1;
case '|' => return (a | b) == 1;
case '^' => return (a ^ b) == 1;
case '>' => return a > b;
case '<' => return a < b;
}
// invalid operator
return false;
}
def brackets(size: Int): Unit = {
var i: Int = 0;
while (i <= size)
{
print("(");
i += 1;
}
}
// Swapping two string elements by index
def swap(start: Int, end: Int): Unit = {
var a: Char = operators.charAt(start);
var b: Char = operators.charAt(end);
operators = operators.substring(0, end) +
a.toString() + operators.substring(end + 1);
operators = operators.substring(0, start) +
b.toString() + operators.substring(start + 1);
}
// Find and Display true boolean expression
def expression(k: Int): Unit = {
if (k > operators.length())
{
return;
}
if (k == operators.length() - 1)
{
var size: Int = 0;
var b: Int = 0;
var a: Int = 0;
var result: Boolean = false;
var i: Int = 1;
while (i < operand.length())
{
if (i == 1)
{
if (operand.charAt(0) == '1')
{
a = 1;
}
if (operand.charAt(i) == '1')
{
b = 1;
}
// Base case
result = operate(a, operators.charAt(size), b);
size += 1;
}
else
{
if (operand.charAt(i) == '1')
{
b = 1;
}
if (result == true)
{
a = 1;
}
result = operate(a, operators.charAt(size), b);
size += 1;
}
b = 0;
a = 0;
i += 1;
}
if (result == true)
{
// Display valid expression
size = 0;
brackets(operand.length() - 2);
var i: Int = 1;
while (i < operand.length())
{
if (i == 1)
{
print(" " + operand.charAt(0) + " " +
operators.charAt(size) + " " +
operand.charAt(i) + " )");
size += 1;
}
else
{
print(" " + operators.charAt(size) + " " +
operand.charAt(i) + " )");
size += 1;
}
i += 1;
}
println();
}
}
var i: Int = k;
while (i < operators.length())
{
swap(k, i);
// recursive call
expression(k + 1);
// back to previous operator position
swap(k, i);
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
// Create instance
var task: BooleanExpression = new BooleanExpression("01010", "|&^>");
}
}``````

#### Output

``````(((( 0 | 1 ) & 0 ) ^ 1 ) > 0 )
(((( 0 | 1 ) ^ 0 ) & 1 ) > 0 )
(((( 0 | 1 ) > 0 ) & 1 ) ^ 0 )
(((( 0 & 1 ) | 0 ) ^ 1 ) > 0 )
(((( 0 & 1 ) ^ 0 ) | 1 ) > 0 )
(((( 0 & 1 ) > 0 ) ^ 1 ) | 0 )
(((( 0 & 1 ) > 0 ) | 1 ) ^ 0 )
(((( 0 ^ 1 ) & 0 ) | 1 ) > 0 )
(((( 0 ^ 1 ) | 0 ) & 1 ) > 0 )
(((( 0 ^ 1 ) > 0 ) & 1 ) | 0 )
(((( 0 > 1 ) & 0 ) ^ 1 ) | 0 )
(((( 0 > 1 ) & 0 ) | 1 ) ^ 0 )``````
``````import Foundation;
// Swift 4 program
// Display all True Bool expression
class BoolExpression
{
var operand: [Character];
var operators: [Character];
init(_ operand: String, _ operators: String)
{
self.operators = Array(operators);
self.operand = Array(operand);
}
func operate(_ a: Int, _ op: Character, _ b: Int) -> Bool
{
// Assume (&^|><) operator are used
if (op == "&")
{
return (a & b) == 1;
}
if (op == "|")
{
return (a | b) == 1;
}
if (op == "^")
{
return (a ^ b) == 1;
}
if (op == ">")
{
return a > b;
}
if (op == "<")
{
return a < b;
}
// invalid operator
return false;
}
func brackets(_ size: Int)
{
var i: Int = 0;
while (i <= size)
{
print("(", terminator: "");
i += 1;
}
}
// Swapping two string elements by index
func swap(_ start: Int, _ end: Int)
{
let a: Character = self.operators[start];
let b: Character = self.operators[end];
self.operators[start] = b;
self.operators[end] = a;

}
// Find and Display true Bool expression
func expression(_ k: Int)
{
if (k > self.operators.count)
{
return;
}
if (k == self.operators.count - 1)
{
var size: Int = 0;
var b: Int = 0;
var a: Int = 0;
var result: Bool = false;
var i: Int = 1;
while (i < self.operand.count)
{
if (i == 1)
{
if (self.operand[0] == "1")
{
a = 1;
}
if (self.operand[i] == "1")
{
b = 1;
}
// Base case
result = self.operate(a, self.operators[size], b);
size += 1;
}
else
{
if (self.operand[i] == "1")
{
b = 1;
}
if (result == true)
{
a = 1;
}
result = self.operate(a, self.operators[size], b);
size += 1;
}
b = 0;
a = 0;
i += 1;
}
if (result == true)
{
// Display valid expression
size = 0;
self.brackets(self.operand.count - 2);
var i: Int = 1;
while (i < self.operand.count)
{
if (i == 1)
{
print("", self.operand[0], "",
self.operators[size], "",
self.operand[i], ")", terminator: "");
size += 1;
}
else
{
print(" ", self.operators[size], "",
self.operand[i], ")", terminator: "");
size += 1;
}
i += 1;
}
print();
}
}
var i: Int = k;
while (i < self.operators.count)
{
self.swap(k, i);
// recursive call
self.expression(k + 1);
// back to previous operator position
self.swap(k, i);
i += 1;
}
}
}
func main()
{
// Create instance
let task: BoolExpression = BoolExpression("01010", "|&^>");
}
main();``````

#### Output

``````(((( 0  |  1 )  &  0 )  ^  1 )  >  0 )
(((( 0  |  1 )  ^  0 )  &  1 )  >  0 )
(((( 0  |  1 )  >  0 )  &  1 )  ^  0 )
(((( 0  &  1 )  |  0 )  ^  1 )  >  0 )
(((( 0  &  1 )  ^  0 )  |  1 )  >  0 )
(((( 0  &  1 )  >  0 )  ^  1 )  |  0 )
(((( 0  &  1 )  >  0 )  |  1 )  ^  0 )
(((( 0  ^  1 )  &  0 )  |  1 )  >  0 )
(((( 0  ^  1 )  |  0 )  &  1 )  >  0 )
(((( 0  ^  1 )  >  0 )  &  1 )  |  0 )
(((( 0  >  1 )  &  0 )  ^  1 )  |  0 )
(((( 0  >  1 )  &  0 )  |  1 )  ^  0 )``````
``````// Kotlin program
// Display all True Boolean expression
class BooleanExpression
{
var operand: String;
var operators: String;
constructor(operand: String, operators: String)
{
this.operators = operators;
this.operand = operand;
}
fun operate(a: Int, op: Char, b: Int): Boolean
{
// Assume (&^|><) operator are used
when (op)
{
'&' -> return (a and b) == 1;
'|' -> return (a or b) == 1;
'^' -> return (a xor b) == 1;
'>' -> return a > b;
'<' -> return a < b;
}
// invalid operator
return false;
}
fun brackets(size: Int): Unit
{
var i: Int = 0;
while (i <= size)
{
print("(");
i += 1;
}
}
// Swapping two string elements by index
fun swap(start: Int, end: Int): Unit
{
var a: Char = this.operators.get(start);
var b: Char = this.operators.get(end);
this.operators = this.operators.substring(0, end) +
a.toString() + this.operators.substring(end + 1);
this.operators = this.operators.substring(0, start) +
b.toString() + this.operators.substring(start + 1);
}
// Find and Display true boolean expression
fun expression(k: Int): Unit
{
if (k > this.operators.length)
{
return;
}
if (k == this.operators.length - 1)
{
var size: Int = 0;
var b: Int = 0;
var a: Int = 0;
var result: Boolean = false;
var i: Int = 1;
while (i < this.operand.length)
{
if (i == 1)
{
if (this.operand.get(0) == '1')
{
a = 1;
}
if (this.operand.get(i) == '1')
{
b = 1;
}
// Base case
result = this.operate(a, this.operators.get(size), b);
size += 1;
}
else
{
if (this.operand.get(i) == '1')
{
b = 1;
}
if (result == true)
{
a = 1;
}
result = this.operate(a, this.operators.get(size), b);
size += 1;
}
b = 0;
a = 0;
i += 1;
}
if (result == true)
{
// Display valid expression
size = 0;
this.brackets(this.operand.length - 2);
i = 1;
while (i < this.operand.length)
{
if (i == 1)
{
print(" " + this.operand.get(0) + " " +
this.operators.get(size) + " " +
this.operand.get(i) + " )");
size += 1;
}
else
{
print(" " + this.operators.get(size) + " " +
this.operand.get(i) + " )");
size += 1;
}
i += 1;
}
println();
}
}
var i = k;
while (i < this.operators.length)
{
this.swap(k, i);
// recursive call
this.expression(k + 1);
// back to previous operator position
this.swap(k, i);
i += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
// Create instance
val task: BooleanExpression = BooleanExpression("01010", "|&^>");
}``````

#### Output

``````(((( 0 | 1 ) & 0 ) ^ 1 ) > 0 )
(((( 0 | 1 ) ^ 0 ) & 1 ) > 0 )
(((( 0 | 1 ) > 0 ) & 1 ) ^ 0 )
(((( 0 & 1 ) | 0 ) ^ 1 ) > 0 )
(((( 0 & 1 ) ^ 0 ) | 1 ) > 0 )
(((( 0 & 1 ) > 0 ) ^ 1 ) | 0 )
(((( 0 & 1 ) > 0 ) | 1 ) ^ 0 )
(((( 0 ^ 1 ) & 0 ) | 1 ) > 0 )
(((( 0 ^ 1 ) | 0 ) & 1 ) > 0 )
(((( 0 ^ 1 ) > 0 ) & 1 ) | 0 )
(((( 0 > 1 ) & 0 ) ^ 1 ) | 0 )
(((( 0 > 1 ) & 0 ) | 1 ) ^ 0 )``````

## Comment

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.

Categories
Relative Post