In: Computer Science
Write a python code without using Sympy and Numpy to derive the polynomial 2x2 + 5x + 4.
B = input('please enter the polynomial of B in the form ( 3x**3 + -5x**4 + 30) : ')
while '+' in B :
index = B.index('+')
B = B.replace('+' , ' ' )
B = B.split()
B_drv = []
def deriv(B):
for parameter in B :
if 'x' in parameter :
index_y = parameter.index('x')
multiply = parameter[0:index_y]
# multiply referes to the number multiplied by 'y' , which is '9' in '9y**2'
if multiply == '' :
multiply = '1' #in case the user wrote 'y**3' instead of '1y**3'
power = parameter[index_y+3:]
# power referes to the power of 'y' , which is '2' in '9y**2'
if power != '0' :
drv_parameter = str(float(multiply) * float(power)) + 'x**' + str(float(power)-1)
else :
drv_parameter = str(0) #in case the user wrote a parameter '4y**0' instead of '4'
B_drv.append(drv_parameter) #filling the list
else: #if there is no 'y' in the parameter
drv_parameter = str(0)
B_drv.append(drv_parameter)
result = ' + '.join(B_drv) #for display
return result
print ('The derivative of B is : ' + deriv(B))
Please upvote and comment incase of doubts