我如何定义一个函数——比如说def polyToString(poly)
——以标准形式返回一个包含多项式的字符串poly
?
例如:由 表示的多项式[-1, 2, -3, 4, -5]
将返回为:
"-5x**4 + 4x**3 -3x**2 + 2x**1 - 1x**0"
def polyToString(poly):
standard_form=''
n=len(poly) - 1
while n >=0:
if poly[n]>=0:
if n==len(poly)-1:
standard_form= standard_form + ' '+ str(poly[n]) + 'x**%d'%n
else:
standard_form= standard_form + ' + '+str(poly[n]) + 'x**%d'%n
else:
standard_form= standard_form + ' - ' + str(abs(poly[n])) + 'x**' + str(n)
n=n-1
return standard_form