Python count down code

From , 5 Years ago, written in Python, viewed 198 times.
URL https://pastebin.vip/view/9c22c0b5
  1. class Expr:  
  2.   def __add__(self, other):  
  3.     return Plus(self, other)  
  4.   def __mul__(self, other):  
  5.     return Times(self, other)  
  6.  
  7. class Int(Expr):  
  8.   def __init__(self, n):  
  9.     self.n = n  
  10.   def d(self, v):  
  11.     return Int(0)  
  12.   def __str__(self):  
  13.     return `self.n`  
  14.  
  15. class Var(Expr):  
  16.   def __init__(self, var):  
  17.     self.var = var  
  18.   def d(self, v):  
  19.     return Int(self.var == v and 1 or 0)  
  20.   def __str__(self):  
  21.     return self.var  
  22.  
  23. class Plus(Expr):  
  24.   def __init__(self, a, b):  
  25.     self.e1 = a  
  26.     self.e2 = b  
  27.   def d(self, v):  
  28.     return Plus(self.e1.d(v), self.e2.d(v))  
  29.   def __str__(self):  
  30.     return "(%s + %s)" % (self.e1, self.e2)  
  31.  
  32. class Times(Expr):  
  33.   def __init__(self, a, b):  
  34.     self.e1 = a  
  35.     self.e2 = b  
  36.   def d(self, v):  
  37.     return Plus(Times(self.e1, self.e2.d(v)), Times(self.e1.d(v), self.e2))  
  38.   def __str__(self):  
  39.     return "(%s * %s)" % (self.e1, self.e2)  
  40.  
  41. if __name__ == "__main__":  
  42.   x = Var("x")  
  43.   a = Var("a")  
  44.   b = Var("b")  
  45.   c = Var("c")  
  46.   e = a * x * x + b * x + c  
  47.   print "d(%s, x) = %s" % (e, e.d("x"))  
  48. #//python/4553

Reply to "Python count down code"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website