Search code examples
pythontuplespolynomials

A couple questions about the way tuples work in Python


So, I was doing this problem from MIT OCW's Introduction to Computer Science and Programming:

Problem #2
Implement the compute_deriv function. This function computes the derivative 
of a polynomial function. It takes in a tuple of numbers poly and returns 
the derivative, which is also a polynomial represented by a tuple.

def compute_deriv(poly):

"""
Computes and returns the derivative of a polynomial function. If the

derivative is 0, returns (0.0,).

Example:

>>> poly = (-13.39, 0.0, 17.5, 3.0, 1.0)    # x4 + 3.0x3 + 17.5x2 - 13.39 

>>> print compute_deriv(poly)               # 4.0x3 + 9.0x2 + 35.0x 

(0.0, 35.0, 9.0, 4.0)

poly: tuple of numbers, length > 0

returns: tuple of numbers

"""

# TO DO ...

And this is my program (it works):

def compute_deriv(poly):
    derivatives=()
    for i in poly:
        if list(poly).index(i)==0:     #my version is 2.5.4, so I cannot use 
            continue                      #tuple.index(i) and since I didn't 
        else:                             #find another way, I converted the 
            deriv=i*list(poly).index(i)           #tuple to a list
            derivatives=derivatives+(deriv,)
    return derivatives
polyx=(-13.39, 0.0, 17.5, 3.0, 1.0)
print compute_deriv(polyx)
polyxx=(1.3, 7.0, 4.0, 2.5, 0.0, -8.0)
print compute_deriv(polyxx)

The first thing is that I wanted for the program to ask me to input the polynomial instead of writing it inside of it:

...
polyx=tuple(raw_input("Enter your polynomial tuple here:"))
print compute_deriv(polyx)

but that didn't work:

Enter your tuple here:-13.39, 0.0, 17.5, 3.0, 1.0
('1', '33', '...', '33', '99999', ',,,,,,', '       ', '00000000', '...', 
'00000000', ',,,,,,', '       ', '1', '77777777777777', '...', 
'5555555555555555', ',,,,,,', '       ', '33', '...', '00000000', ',,,,,,', 
' ', '1', '...', '00000000')

Why? And the other problem is with the second tuple (-8x^5+2.5x^3+4x^2+7x+1.3) - when its members are respectively (1.3, 7.0, 4.0, 2.5, 0.0, -8.0), it returns as expected - (7.0, 8.0, 7.5, 0.0, -40.0), but if the first member is 0.0 (as in -8x^5+2.5x^3+4x^2+7x), that changes - (7.0, 8.0, 7.5, -40.0). The second 0.0 is omitted and that's a problem as it implies that the power of the -40.0 is 3 when it is 4. Again, why?

Thank you for your time!


Solution

  • raw_input will convert your input to a string. Thus, entering -13.39, 0.0, 17.5, 3.0, 1.0 will result in a string.

    Then you convert it to a tuple. By default, when given a string, tuple will split each character in the string and form a tuple with all of them. Example:

    tuple("Hello World")
    

    Output: ('H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd')

    That's why you get those results. Possible solution: input the tuple separated by commas ',' and then split it:

    polyx = tuple(raw_input("Enter your polynomial tuple here: ").split(","))
    

    Example:

    Enter your polynomial tuple here: -13.39,0.0,17.5,3.0,1.0
    

    Output: (u'-13.39', u'0.0', u'17.5', u'3.0', u'1.0')

    Notice though, that this tuple will contain strings, not floats. You need to convert them:

    polyx = tuple(float(x) for x in raw_input("Enter your polynomial tuple here: ").split(","))
    

    Example:

    Enter your polynomial tuple here: -13.39, 0.0, 17.5, 3.0, 1.0
    

    Output: (-13.39, 0.0, 17.5, 3.0, 1.0)