Create a function that differentiates a polynomial for a given value of `x`.

Your function will receive 2 arguments: a polynomial as a string, and a point to evaluate the equation as an integer.

## Assumptions:

* There will be a coefficient near each `x`, unless the coefficient equals `1` or `-1`.
* There will be an exponent near each `x`, unless the exponent equals `0` or `1`.
* All exponents will be greater or equal to zero

## Examples:

```python
differenatiate("12x+2", 3)      ==>   returns 12
differenatiate("x^2+3x+2", 3)   ==>   returns 9
```

Difficulty: MEDIUM
Tags: String algorithms

Fundamentals

Starter code:
def differentiate(equation, point):
	

Solutions:
from collections import defaultdict
import re
P = re.compile('\\+?(-?\\d*)(x\\^?)?(\\d*)')

def differentiate(eq, x):
	derivate = defaultdict(int)
	for (coef, var, exp) in P.findall(eq):
		exp = int(exp or (var and '1') or '0')
		coef = int(coef != '-' and coef or (coef and '-1') or '1')
		if exp:
			derivate[exp - 1] += exp * coef
	return sum((coef * x ** exp for (exp, coef) in derivate.items()))


def parse_monom(monom):
	if 'x' not in monom:
		monom = monom + 'x^0'
	if monom.startswith('x'):
		monom = '1' + monom
	if monom.startswith('-x'):
		monom = '-1' + monom[1:]
	if monom.endswith('x'):
		monom = monom + '^1'
	(coefficient, degree) = map(int, monom.replace('x', '').split('^'))
	return (degree, coefficient)

def differentiate(equation, point):
	monoms = equation.replace('-', '+-').lstrip('+').split('+')
	polynom = dict(map(parse_monom, monoms))
	return sum((coefficient * degree * point ** (degree - 1) for (degree, coefficient) in polynom.items() if degree))


from re import finditer

def differentiate(equation, point):
	res = 0
	for exp in finditer('([\\+\\-])?(\\d*)?x\\^?(\\d+)?', equation):
		sign = -1 if exp.group(1) == '-' else 1
		scalar = int(exp.group(2)) if exp.group(2) else 1
		power = int(exp.group(3)) if exp.group(3) else 1
		res += sign * (power * scalar) * point ** (power - 1)
	return res


def differentiate(equation, point):
	signs = [i for i in range(len(equation)) if equation[i] in '+-']
	if not 0 in signs:
		signs = [0] + signs
	coeffs = []
	powers = []
	for i in range(len(signs)):
		if i + 1 == len(signs):
			term = equation[signs[i]:]
		else:
			term = equation[signs[i]:signs[i + 1]]
		if '^' in term:
			i = term.index('^')
			powers += [int(term.split('^')[1])]
		elif 'x' in term:
			powers += [1]
		if 'x' in term:
			c = term.split('x')[0]
			if c in ['', '+', '-']:
				coeffs += [-1 if c == '-' else 1]
			else:
				coeffs += [int(c)]
	return sum([c * p * point ** (p - 1) for (c, p) in zip(coeffs, powers)])


import re

def differentiate(equation, point):
	d = {int(p or 1): int({'': 1, '-': -1}.get(n, n)) for (n, x, p) in re.findall('([-]?\\d*)(?:(x)(?:\\^(\\d+))?)?', equation) if x or p}
	return sum((t * p * point ** (p - 1) for (p, t) in d.items()))


def differentiate(equation, point):
	a = filter(lambda x: 'x' in x, equation.replace('-', '+-').split('+'))
	sum = 0
	for i in list(a):
		split_i = i.split('x')
		split_i[0] = split_i[0] + '1' if split_i[0] == '' or split_i[0] == '-' else split_i[0]
		split_i[1] = '^1' if split_i[1] == '' else split_i[1]
		sum += int(split_i[0]) * int(split_i[1][1:]) * point ** (int(split_i[1][1:]) - 1)
	return sum


def differentiate(eq, point):
	eq = eq.replace('-', ' -').strip()
	eq = eq.replace('+', ' +').strip()
	eq = eq.replace('-x', '-1x')
	eq = eq.replace('+x', '+1x')
	eq = eq.split(' ')
	eq = [i if i[0].isalpha() == False else '1' + i for i in eq]
	if 'x' not in eq[-1]:
		eq.pop(-1)
	for i in range(len(eq)):
		if eq[i][-1] == 'x':
			eq[i] = eq[i][:-1] + 'x^1'
		j = int(eq[i][-1])
		eq[i] = int(eq[i][:-3]) * j * point ** (j - 1)
	return sum(eq)


import re

def differentiate(equation, point):
	terms = re.findall('[-]*[0-9x^]+', equation)
	result = 0
	for term in terms:
		amount = re.search('^-*[0-9]+|^-*x', term)
		if amount is not None:
			amount = amount.group(0)
			if amount == 'x':
				amount = 1
			elif amount == '-x':
				amount = -1
			else:
				int(amount)
		power = re.search('(?<=\\^)\\d+$|x+$', term)
		if power is not None:
			power = power.group(0)
			if power.isnumeric():
				result = result + int(power) * int(amount) * point ** (int(power) - 1)
			elif power == 'x':
				power = 1
				result = result + int(amount) * power
	return result


from re import findall

def val(s):
	if not s:
		return 1
	if s[0] == '+':
		return int(s[1:] or '1')
	if s[0] == '-':
		return -int(s[1:] or '1')
	return int(s)

def differentiate(equation, point):
	res = 0
	for x in findall('[+-]?\\d*x(?:\\^\\d+)?', equation):
		i = x.find('x')
		(v, p) = (val(x[:i]), int(x[i + 2:] or '1'))
		res += v * p * point ** (p - 1)
	return res


def differentiate(equation, point):
	if equation[0] != '-':
		equation = '+' + equation
	equation = equation.replace('+x', '+1x').replace('-x', '-1x').replace('+', ' ').replace('-', ' -')
	terms = [[int(e) for e in (w + '^1' if '^' not in w else w).split('x^')] for w in equation.split() if 'x' in w]
	return sum((c * p * point ** (p - 1) for (c, p) in terms))