Facebook
From gal, 3 Years ago, written in Python.
Embed
Download Paste or View Raw
Hits: 53
  1. class Term:
  2.     def __init__(self, variable=None, const=None):
  3.         self.variable = variable
  4.         self.const = const
  5.         self.tree = []
  6.  
  7.     @classmethod
  8.     def const(cls, const):
  9.         term = cls(None, const)
  10.         return term
  11.  
  12.     @classmethod
  13.     def var(cls, variable):
  14.         term = cls(variable, None)
  15.         return term
  16.  
  17.     def __call__(self, **kwargs):
  18.         return Term.calculate(Term.exchange(self.tree, **kwargs))
  19.  
  20.     def __add__(self, other):
  21.         term = Term()
  22.         term.tree.append("+")
  23.         term.tree.append(self.variable)
  24.         if len(other.tree) is not 0:
  25.             term.tree.append(other.tree)
  26.         else:
  27.             term.tree.append(other.variable)
  28.         return term
  29.  
  30.     def __sub__(self, other):
  31.         term = Term()
  32.         term.tree.append("-")
  33.         term.tree.append(self.variable)
  34.         if len(other.tree) is not 0:
  35.             term.tree.append(other.tree)
  36.         else:
  37.             term.tree.append(other.variable)
  38.         return term
  39.  
  40.     def __mul__(self, other):
  41.         term = Term()
  42.         term.tree.append("*")
  43.         term.tree.append(self.variable)
  44.         if len(other.tree) is not 0:
  45.             term.tree.append(other.tree)
  46.         else:
  47.             term.tree.append(other.variable)
  48.         return term
  49.  
  50.     def __str__(self):
  51.         return Term.to_string(self.tree)
  52.  
  53.     @staticmethod
  54.     def exchange(tree, **kwargs):
  55.         if isinstance(tree[1], str) or isinstance(tree[2], str):
  56.             if isinstance(tree[1], str):
  57.                 for key, value in kwargs.items():
  58.                     if tree[1] == str(key):
  59.                         tree[1] = value
  60.             if isinstance(tree[2], str):
  61.                 for key, value in kwargs.items():
  62.                     if tree[2] == str(key):
  63.                         tree[2] = value
  64.             return (tree[0], tree[1], tree[2])
  65.         elif isinstance(tree[2], tuple):
  66.             return Term.exchange((tree[0], tree[1], Term.exchange(tree[2])))
  67.  
  68.     @staticmethod
  69.     def calculate(tree):
  70.         if isinstance(tree[2], int):
  71.             if tree[0] == "+":
  72.                 return tree[1] + tree[2]
  73.             elif tree[0] == "-":
  74.                 return tree[1] - tree[2]
  75.             elif tree[0] == "*":
  76.                 return tree[1] * tree[2]
  77.         else:
  78.             return Term.calculate((tree[0], tree[1], Term.calculate(tree[2])))
  79.  
  80.     @staticmethod
  81.     def to_string(tree):
  82.         if not isinstance(tree[2], list):
  83.             return "({0} {1} {2})".format(tree[1], tree[0], tree[2])
  84.         else:
  85.             return Term.to_string((tree[0], tree[1], Term.to_string(tree[2])))
  86.  
  87.  
  88. if __name__ == "__main__":
  89.     x = Term.var("x")
  90.     y = Term.var("y")
  91.     z = Term.var("z")
  92.     d = x + y
  93.     print(d(x=1, y=2))
  94.