Home > Software design >  Smart ways to handle boolean and string
Smart ways to handle boolean and string

Time:08-07

I want to know what is the smart way to handle Boolean and string together. Let say I have the following Boolean expression "~a" (not a) with respect to a trace T = ["ab", "b", "a", ""]. The evaluation result is R = [False, True, False, True] because it must not have a. but while parsing if I have True instead of a then the trace will change. I have written the condition in python like this,

    if op == '~':
        if p == True:
            return False
        elif p == False:
            return True
        if p in T[i]:
            return False
        else:
            return True

Is there any succinct and better way to write this? (p can be a string or a boolean value) I tried to short it as it is a simple condition, but I'm facing the error that says string and boolean do not match.

CodePudding user response:

let see... do you mean something like this?

def setter(data, T):
    if data[0] == "~":
        data = data[1:]
        return [data not in t for t in T]
    else:
        return [data in t for t in T]
    
setter("a", ["ab", "b", "a", ""])
  • Related