Day 18: Operation Order¶
Classes:
|
Represent a mathematical operation. |
|
Represent the tail of an expression. |
|
Represent a node of the abstract syntax tree of a mathematical expression. |
Functions:
|
Extract the sub-expression surrounded by parentheses in |
|
Parse the |
|
Serialize the abstraction syntax tree given as |
|
Evaluate the parsed expression given as |
- class Operation(value)[source]¶
Represent a mathematical operation.
Attributes:
Addition
Multiplication
- ADD = '+'¶
Addition
- MUL = '*'¶
Multiplication
- class Tail(op: Operation, right: Union[int, Node])[source]¶
Represent the tail of an expression.
Attributes:
Operation of the mathematical expression
Right-hand side of the expression
Methods:
__delattr__
(name)Implement delattr(self, name).
__eq__
(other)Return self==value.
__init__
(op, right)__repr__
()Return repr(self).
__setattr__
(name, value)Implement setattr(self, name, value).
- __delattr__(name)¶
Implement delattr(self, name).
- __eq__(other)¶
Return self==value.
- __repr__()¶
Return repr(self).
- __setattr__(name, value)¶
Implement setattr(self, name, value).
- class Node(head: Union[int, Node], tail: List[Tail])[source]¶
Represent a node of the abstract syntax tree of a mathematical expression.
Attributes:
Constant or left-hand side expression
Remaining expressions on the right-hand side
Methods:
__delattr__
(name)Implement delattr(self, name).
__eq__
(other)Return self==value.
__init__
(head, tail)__repr__
()Return repr(self).
__setattr__
(name, value)Implement setattr(self, name, value).
- __delattr__(name)¶
Implement delattr(self, name).
- __eq__(other)¶
Return self==value.
- __repr__()¶
Return repr(self).
- __setattr__(name, value)¶
Implement setattr(self, name, value).
- extract_expression(expr: str) str [source]¶
Extract the sub-expression surrounded by parentheses in
expr
.- Requires
expr.count("(") == expr.count(")")
re.match(r"^(.+)", expr)
- Ensures
result in expr