[Tutor] attaching the first element in a list to a string

Julieta Rangel julieta_rangel@hotmail.com
Wed, 02 May 2001 19:17:11 -0500


I have this problem.  I'm trying to write a program that calculates the 
integral of a polynomial.  This is what I have so far:

import string
class Poly:
    def __init__ ( self, v='x' , c = [0]):
        """__init__():
        Initializes a polynomial
        Default variable is x
        Default polynomial is zero"""
        self.var = v
        self.coef = c
        self.deg = len(c)-1
        self.length = len(c)
    def __str__ (self):
        """__str__():
        Converts a polynomial into a string"""
        x = `self.coef[0]`
        for i in range (1, self.deg+1):
            x = x + "+" +`self.coef[i]` + self.var + "^" + `i`
        return x

class Poly2:
    def __init__ (self, v='x' , c = [0]):
        """__init__():
        Initializes a polynomial
        Default variable is x
        Default polynomial is zero"""
        self.var = v
        self.pc = c
        self.deg =len(c)-1
        self.length = len(c)
    def __str__(self):
        """__str__():
        Converts a second polynomial into a string"""
        x = `self.pc[0]`
        for i in range (0, self.deg+1):
            x = x + "+" +`self.pc[i]`+ self.var + "^" +`i+2`
        return x

def Integrate(p):
    """Input:  an instance of a polynommial
    Output: a polynomial that is the integral of the input polynomial
    Side Effects: None"""
    pv=p.var
    pc=[]
    for i in range(0,p.deg):
     	I=p.coef[i+1]/float(i+2)
      	pc.append(I)
    return Poly2 (v=pv,c=pc)


degree = input('Enter the degree of your polynomial')
coef=[]
for i in range (0,degree + 1):
    coefficient = input('Enter the coefficient for x^ %i ? ' %i)
    coef.append(coefficient)#attach the coefficient entered to the end of 
the
    #list named coef.


s= Poly (c = coef)
t= Integrate(s)
# This is some code to test new functions

print "The integral of %s is: \n %s" % (s,t)
print " "

My problem is that when I run it I get this:

>>>reload(poly1)
Enter the degree of your polynomial3
Enter the coefficient for x^ 0 ? 2
Enter the coefficient for x^ 1 ? 5
Enter the coefficient for x^ 2 ? 6
Enter the coefficient for x^ 3 ? 1
The integral of 2+5x^1+6x^2+1x^3 is:
2.5+2.5x^2+2.0x^3+0.25x^4

As you can see, the answer would be right if it weren't for the first term 
in the polynomial.  The first term should be a 2x, so the answer should look 
like this:  2x^1+2.5x^2+2x^3+0.25x^4.  Can anyone help me fix this problem?  
I think that I need to find a way to have the first term on the list labeled 
pc, which is 2 to be taken into consideration on the string representing the 
polynomial.  Am I right?  If so, How can I accomplish this?  Also, I need to 
find a way so that when the coefficient of x^0 ==0 , the output should 
display a constant c along with the result, so that the answer looks like 
c+2.5x^2+2x^3+0.23x^4.
Can anyone guide me in the right direction?  All inputs or comments about my 
program are welcomed and appreciated.

Julieta

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com