CoCubes Coding Questions – 5

Given a string consisting of only 0, 1, A, B, C where
A = AND
B = OR
C = XOR
Calculate the value of the string assuming no order of precedence and evaluation is done from left to right.

Constraints – The length of string will be odd. It will always be a valid string.
Example, 1AA0 will not be given as an input.

Examples:

Input : 1A0B1
Output : 1
1 AND 0 OR 1 = 1

Input : 1C1B1B0A0
Output : 0

C/C++

// C++ program to evaluate value of an expression.
#include <bits/stdc++.h>
using namespace std;

int evaluateBoolExpr(string s)
{
int n = s.length();

// Traverse all operands by jumping
// a character after every iteration.
for (int i = 0; i < n; i += 2) {

// If operator next to current operand
// is AND.
if (s[i + 1] ==’A’) {
if (s[i + 2] ==’0’|| s[i] ==’0’)
s[i + 2] =’0’;
else
s[i + 2] =’1’;
}

// If operator next to current operand
// is OR.
else if (s[i + 1] ==’B’) {
if (s[i + 2] ==’1’|| s[i] ==’1’)
s[i + 2] =’1’;
else
s[i + 2] =’0’;
}

// If operator next to current operand
// is XOR (Assuming a valid input)
else {
if (s[i + 2] == s[i])
s[i + 2] =’0’;
else
s[i + 2] =’1’
}
}
return s[n – 1] -‘0’;
}

// Driver code
int main()
{
string s = “1C1B1B0A0”;
cout << evaluateBoolExpr(s);
return 0;
}

Java

// Java program to evaluate value of an expression.
public class Evaluate_BoolExp {

// Evaluates boolean expression
// and returns the result
static int evaluateBoolExpr(StringBuffer s)
{
int n = s.length();

// Traverse all operands by jumping
// a character after every iteration.
for (int i = 0; i < n; i += 2) {

// If operator next to current operand
// is AND.
if( i + 1 < n && i + 2 < n)
{
if (s.charAt(i + 1) == ‘A’) {
if (s.charAt(i + 2) == ‘0’ ||
s.charAt(i) == 0)
s.setCharAt(i + 2, ‘0’);
else
s.setCharAt(i + 2, ‘1’);
}

// If operator next to current operand
// is OR.
else if ((i + 1) < n &&
s.charAt(i + 1 ) == ‘B’) {
if (s.charAt(i + 2) == ‘1’ ||
s.charAt(i) == ‘1’)
s.setCharAt(i + 2, ‘1’);
else
s.setCharAt(i + 2, ‘0’);
}

// If operator next to current operand
// is XOR (Assuming a valid input)
else {
if (s.charAt(i + 2) == s.charAt(i))
s.setCharAt(i + 2, ‘0’);
else
s.setCharAt(i + 2 ,’1′);
}
}
}
return s.charAt(n – 1) – ‘0’;
}

// Driver code
public static void main(String[] args)
{
String s = “1C1B1B0A0”;
StringBuffer sb = new StringBuffer(s);
System.out.println(evaluateBoolExpr(sb));
}
}

5 comments on “CoCubes Coding Questions – 5”


  • Shruti

    public class MyClass {
    public static void main(String args[]) {
    String s=”1C1B1B0A0″;
    int min=Character.getNumericValue(s.charAt(0));
    for(int i=1;i<s.length()-1;i++){
    if(s.charAt(i)=='A'){
    min=min & Character.getNumericValue(s.charAt(i+1));
    }
    else if(s.charAt(i)=='B'){
    min=min | Character.getNumericValue(s.charAt(i+1));
    }
    else if(s.charAt(i)=='C'){
    min=min ^ Character.getNumericValue(s.charAt(i+1));
    }
    }
    System.out.println(min);
    }
    }


  • Atharv

    #include
    using namespace std;

    int main(){
    string s;
    cin>>s;

    int ans=1;

    for(int i=0;i<s.length()-1;i++){

    if(s[i]!='A' && s[i]!='B' && s[i]!='C'){
    ans=ans*int(s[i]-'0');
    }

    if(s[i]=='A'){
    ans=ans&int(s[i+1]-'0');
    i++;
    }

    if(s[i]=='B'){
    ans=ans|int(s[i+1]-'0');
    i++;
    }

    if(s[i]=='C'){
    ans=ans^int(s[i+1]-'0');
    i++;
    }

    }
    cout<<ans;
    return 0;
    }


  • shubham

    s = list(input())
    l = len(s)
    while(len(s)>1):
    x = int(s.pop())
    op = s.pop()
    y = int(s.pop())
    if (op==’A’):
    t = x & y
    elif(op==’B’):
    t = x|y
    elif(op==’C’):
    t = x^y
    s.append(t)
    print(t)


  • Varshiny

    s=input()
    l=list(s)
    for j in range(0,len(l)):
    for i in range(1,len(l),2):
    if(l[i]==’A’):
    m=int(l[i-1]) * int(l[i+1])
    elif(l[i]==’B’):
    m=int(l[i-1]) + int(l[i+1])
    elif(l[i]==’C’):
    m=int(l[i-1]) + int(l[i+1])
    if(m==0):
    m=1
    else:
    m=0
    print(m)