Printing a polynomial in python

前端 未结 4 1541
悲&欢浪女
悲&欢浪女 2021-01-16 18:16

I\'m making a Polynomial python class and as part of that, I need to print a polynomial nicely. The class is given a list that represents the coefficients of the polynomial

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-16 18:54

    There are several issues:

    • you should start your range at 0 and not 1;
    • the power should be len(self.coeffs)-1-i;
    • try also to handle particular cases as numerical comparisons rather than strings substitutions (it is more efficient to work with numerical values rather than converting them to string and then detect things on strings).

    I would personnaly handle [2,3,4] as 2+3x+4x^2 since it would be easier to handle more complicated computations later, but you may have good reasons to want the contrary.

提交回复
热议问题