-2

I am creating a program, which through differentiation, can return the coordinates and nature of any stationary points, given an equation.

My whole program so far is:

equation = input("y=")

expression_list = []

for i in equation:
    expression_list.append(i)


def derive(expression):
    
    global derivation
    derivation = []


    for i in expression:
        if i == "x" and int(expression[expression.index(i)+2]) != 0:

            derivation.append(int(expression[expression.index(i)-1]) * int(expression[expression.index(i)+2]))
            derivation.append("x")
            derivation.append("^")
            derivation.append(int(expression[expression.index(i)+2]) - 1)

derive(expression_list)

expression_list_1st_derivative = derivation

print(expression_list_1st_derivative)

The area of the program that I am having trouble with is:

def derive(expression):
    
    global derivation
    derivation = []


    for i in expression:
        if i == "x" and int(expression[expression.index(i)+2]) != 0:

            derivation.append(int(expression[expression.index(i)-1]) * int(expression[expression.index(i)+2]))
            derivation.append("x")
            derivation.append("^")
            derivation.append(int(expression[expression.index(i)+2]) - 1)

derive(expression_list)

expression_list_1st_derivative = derivation

print(expression_list_1st_derivative)

Using y=4x^5+3x^4 as an example, I have created this for loop in the hope of giving ["20","x","^","4","12","x","^","3"] which, if you have studied calculus, you will know as x/dx. I have not included operators yet; that being something I will work on after clearing this hurdle. Currently these lines of code instead give ["20","x","^","4","20","x","^","4"]. What changes must I make to get the correct result?

Qiu YU
  • 517
  • 4
  • 20
  • `expression.index(i)` returns the index of the first occurrence of `i`, not the index that you're currently thinking of. – khelwood Aug 05 '20 at 10:50
  • See [Accessing the index in 'for' loops?](https://stackoverflow.com/q/522563/3890632) – khelwood Aug 05 '20 at 10:51

1 Answers1

0

Your expression.index(i) is finding the first position of i, not the one that you are currently processing.

Instead you can use enumerate and then you have the current index number available.

It would also be better if your function returned a value (here, a list) instead of setting a global variable:

def derive(expression):
    derivation = []
    for index, i in enumerate(expression):  # <== note use of "enumerate"
        if i == "x" and int(expression[index+2]) != 0:
            derivation.append(int(expression[index-1]) * int(expression[index+2]))
            derivation.append("x")
            derivation.append("^")
            derivation.append(int(expression[index+2]) - 1)
    return derivation  # <== note return value

expression_list = ["20","x","^","4","12","x","^","3"]  
expression_list_1st_derivative = derive(expression_list)
print(expression_list_1st_derivative)
alani
  • 12,573
  • 2
  • 13
  • 23