def print_poly(p):
"""
>>> print_poly([4, 3, 2])
4x^2 + 3x + 2
>>> print_poly([6, 0, 5])
6x^2 + 5
>>> print_poly([7, 0, -3, 5])
7x^3 - 3x + 5
>>> print_poly([1, -1, 0, 0, -3, 2])
x^5 - x^4 - 3x + 2
"""
printable = ''
for i in range(len(p) -1, -1, -1):
poly += ('p[i]' + 'x^' + str(i))
for item in printable:
if 0 in item:
item *= 0
printable += poly[0]
for item in poly[1:]:
printable += item
print(printable)
无论我尝试多少次,我都无法让所有的文档测试都通过。