Verification: a143cc29221c9be0

Php boolean string to boolean

Содержание

C++

#include

using namespace std;

int evaluateBoolExpr(string s)

{

    int n = s.length();

    for (int i = 0; i

        if (s[i + 1] == 'A') {

            if (s[i + 2] == '0'|| s[i] == '0')

                s[i + 2] = '0';

            else

                s[i + 2] = '1';

        }

        else if (s[i + 1] == 'B') {

            if (s[i + 2] == '1'|| s[i] == '1')

                s[i + 2] = '1';

            else

                s[i + 2] = '0';

        }

        else {

            if (s[i + 2] == s[i])

                s[i + 2] = '0';

            else

                s[i + 2] = '1';

        }

    }

    return s[n - 1] -'0';

}

int main()

{

    string s = "1C1B1B0A0";

    cout

    return 0;

}

Java

public class Evaluate_BoolExp {

    static int evaluateBoolExpr(StringBuffer s)

    {

        int n = s.length();

        for (int i = 0; i 2) {

            if( i + 1 2

            {

                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');

                }

                else if ((i + 1)

                           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');

                }

                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';

    }

    public static void main(String[] args)

    {

        String s = "1C1B1B0A0";

        StringBuffer sb = new StringBuffer(s);

        System.out.println(evaluateBoolExpr(sb));

    }

}

Python3

import math as mt

def evaluateBoolExpr(s):

    n = len(s)

    for i in range(0, n - 2, 2):

        if (s[i + 1] == "A"):

            if (s[i + 2] == "0" or s[i] == "0"):

                s[i + 2] = "0"

            else:

                s[i + 2] = "1"

        elif (s[i + 1] == "B"):

            if (s[i + 2] == "1" or s[i] == "1"):

                s[i + 2] = "1"

            else:

                s[i + 2] = "0"

        else:

            if (s[i + 2] == s[i]):

                s[i + 2] = "0"

            else:

                s[i + 2] = "1"

    return ord(s[n - 1]) - ord("0")

s = "1C1B1B0A0"

string=[s[i] for i in range(len(s))]

print(evaluateBoolExpr(string))

C#

using System;

using System.Text;

class GFG

{

public static int evaluateBoolExpr(StringBuilder s)

{

    int n = s.Length;

    for (int i = 0; i

    {

        if (i + 1

        {

            if (s[i + 1] == 'A')

            {

                if (s[i + 2] == '0' || s[i] == 0)

                {

                    s[i + 2] = '0';

                }

                else

                {

                    s[i + 2] = '1';

                }

            }

            else if ((i + 1) 'B')

            {

                if (s[i + 2] == '1' || s[i] == '1')

                {

                    s[i + 2] = '1';

                }

                else

                {

                    s[i + 2] = '0';

                }

            }

            else

            {

                if (s[i + 2] == s[i])

                {

                    s[i + 2] = '0';

                }

                else

                {

                    s[i + 2] = '1';

                }

            }

        }

    }

    return s[n - 1] - '0';

}

public static void Main(string[] args)

{

    string s = "1C1B1B0A0";

    StringBuilder sb = new StringBuilder(s);

    Console.WriteLine(evaluateBoolExpr(sb));

}

}

PHP

function evaluateBoolExpr($s)

{

    $n = strlen($s);

    for ($i = 0; $i $n; $i += 2)

    {

        if (($i + 1) $n  && $s[$i + 1] == 'A')

        {

            if ($s[$i + 2] == '0'|| $s[$i] == '0')

                $s[$i + 2] = '0';

            else

                $s[$i + 2] = '1';

        }

        else if (($i + 1) $n  && $s[$i + 1] == 'B')

        {

            if ($s[$i + 2] == '1'|| $s[$i] == '1')

                $s[$i + 2] = '1';

            else

                $s[$i + 2] = '0';

        }

        else

        {

            if (($i + 2) $n  && $s[$i + 2] == $s[$i])

                $s[$i + 2] = '0';

            else

                $s[$i + 2] = '1';

        }

    }

    return $s[$n - 1] -'0';

}

$s = "1C1B1B0A0";

echo evaluateBoolExpr($s);