I'm struggling to create the __ str __
function (aka pretty print) with polynomials, where dictionaries are used to contain the powers as keys and the elements as coefficients. I have done it with lists but I haven't mastered dictionaries yet. Is there anything to improve?
You can see in the second polynomial that if my last constant is not a constant, after arranging the keys with the reverse()
function, the plus is always there, what can i do to prevent that? By the way I am trying to overload operators, after I've done this I'll try to do __ add__
, __ mul__
, __ sub__
, and __ call__
... though I would finish this one first :P
class Polynomial(object):
def __init__(self, coefficients):
self.coefficients = coefficients
def __str__(self):
polyd = self.coefficients
exponent = polyd.keys()
exponent.reverse()
polytostring = ' '
for i in exponent:
exponent = i
coefficient = polyd[i]
if i == 0:
polytostring += '%s' % coefficient
break
polytostring += '%sx^%s + ' % (coefficient, exponent)
return polytostring
dict1 = {0:1,1:-1}
p1 = Polynomial(dict1)
dict2 = {1:1,4:-6,5:-1, 3:2}
p2 = Polynomial(dict2)
print p1
print p2
for
loop will end(break) when exponent value is equal to0
.code:
Output:
Something like this seems to work, if I understand your problem:
It just sorted the (key,val) pairs by key, then formats each term, and joins the terms into a single string.
This is compact
and works...
Let's have a look at the expression that is
return
ed, a piece at a timeOne of the string methods is
.join()
that accepts a sequence of strings and joins them with the (in this case) null string, e.g.In our case the argument of
join
isthat, parenthesized, is a generator expression, that, btw, is similar to an implicit
for
loop.On the right there is
that assign to the local variable
e
, in turn, thekeys
ofself.coefficients
in sorted and reversed orderand on the left there is the result of the generator expression, evaluated for each possible value of
e
The expression above is known as string formatting or interpolation and works like this,
the string on the left is a format string and the parts in it prefixed by
%
are _format specifiers, here%+g
means generic formatting always prefixed by the sign and%d
means integer digit, whats outside (here `x^``) is copied vebatim into the result,the
%
in the middle is the formatting operator itselfthe tuple
(self.coefficients[e], e)
is the argument of the format string, you mnust have a 1 to 1 correspondance between format specifiers and argumentsAt this point we have all the pieces in place... in a more colloquial form it could be