Non-terminal expressions combine other expressions. Each holds child expressions and delegates to them inside interpret.
Implement:
Because children are themselves expressions, you can nest And, Or, Var and Const to any depth.
class Var:
def __init__(self, name):
self.name = name
def interpret(self, context):
return bool(context.get(self.name, False))
class Const:
def __init__(self, value):
self.value = value
def interpret(self, context):
return self.value
class And:
def __init__(self, a, b):
self.a = a
self.b = b
def interpret(self, context):
"""Return the logical AND of the two child results."""
# TODO: return self.a.interpret(context) and self.b.interpret(context)
pass
class Or:
def __init__(self, a, b):
self.a = a
self.b = b
def interpret(self, context):
"""Return the logical OR of the two child results."""
# TODO: return self.a.interpret(context) or self.b.interpret(context)
pass
The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
Non-terminal expressions combine other expressions. Each holds child expressions and delegates to them inside interpret.
Implement:
Because children are themselves expressions, you can nest And, Or, Var and Const to any depth.
Press Run Tests or ββ© to check your solution.