From 33220c2a3cd09a22e6eae413c3f3e3707556598a Mon Sep 17 00:00:00 2001 From: lafrite Date: Tue, 28 Jan 2014 21:04:27 +0100 Subject: [PATCH] improve printing ofr FormalExp --- formal.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/formal.py b/formal.py index a9f34f0..4e4a98c 100644 --- a/formal.py +++ b/formal.py @@ -134,13 +134,27 @@ class FormalExp(object): return len(list(self._coef.keys())) def __str__(self): - return " + ".join([str(v) + str(k) for k,v in self._coef.items()]) + ans = "" + for k,v in self._coef.items(): + if v < 0: + ans += "-" + else: + ans += "+" + + if abs(v) == 1: + ans += str(k) + else: + ans += str(abs(v)) + str(k) + if ans[0] == "+": + return ans[1:] + else: + return ans if __name__ == '__main__': - fe1 = FormalExp({"x": 1, "":2}) - print(fe1.get_postfix()) + fe1 = FormalExp({"x": -1, "":-2}) + print(fe1) fe2 = FormalExp({"x^12": 5, "":2}) - print(fe2.get_postfix()) + print(fe2) fe3 = fe1 * fe2 for s in fe3: print(s)