From 85d3c4a3c655dbacc7120a7b07a3862bad48bd24 Mon Sep 17 00:00:00 2001 From: hosein Date: Sun, 6 Oct 2019 15:32:52 +0330 Subject: [PATCH] oop good example. --- OOP-Polynomial_equation-example.py | 39 ++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 OOP-Polynomial_equation-example.py diff --git a/OOP-Polynomial_equation-example.py b/OOP-Polynomial_equation-example.py new file mode 100644 index 0000000..165fe6c --- /dev/null +++ b/OOP-Polynomial_equation-example.py @@ -0,0 +1,39 @@ +class Poly(object): + def __init__(self, coeff): + self.coeff = coeff + + def __str__(self): + repres = 'P(x)= ' + l = len(self.coeff) + for idx, c in enumerate(self.coeff): + if (c > 0): + repres += f'+{c}X^{l-1-idx}' + elif(c < 0): + repres += f'{c}X^{l-1-idx}' + elif (c == 0): + pass + return repres + + def evaluate(self, x0): + s = 0 + l = len(self.coeff) + for idx, c in enumerate(self.coeff): + p = l - 1 - idx + s += c*x0**p + + return s + + def __call__(self, x0,): + s = 0 + l = len(self.coeff) + for idx, c in enumerate(self.coeff): + p = l - 1 - idx + s += c*x0**p + + return s + + def __len__(self): + return len(self.coeff) + + def __getitem__(self, idx): + return self.coeff[idx]