Created
November 18, 2022 21:25
-
-
Save breandan/ebf50440cd2799adb8605f461b86a41b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import annotations | |
from functools import reduce | |
from typing import Union, SupportsFloat | |
class Operator: | |
def __str__(self): | |
return "" | |
class NoOP(Operator): | |
def __str__(self): | |
return "" | |
class Plus(Operator): | |
def __str__(self): | |
return "+" | |
class Times(Operator): | |
def __str__(self): | |
return "*" | |
class Formula: | |
def __init__(self): | |
self.op = NoOP | |
def __init__(self, left: Formula, right: Formula, op: Operator = NoOP): | |
self.left, self.right= left, right | |
self.op = op | |
def __str__(self): | |
return "" if(self.op == NoOP()) else str(self.left) + ("+" if self.op is Plus else "*") + str(self.right) | |
class Const(Formula): | |
def __init__(self, i): | |
self.i = i | |
def __str__(self): | |
return f'{self.i}' | |
# print() | |
print(eval(str(Formula(left=Const("a"),right=Formula(left=Const(2), right=Const(2), op=Times),op=Times)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment