0

Testing the polynomial '2x^3+4x^2+8x-16' my code below outputs [6, 8] as the coefficients of the differentiated polynomial. However, the output should be [6, 8, 8]. Why is the function getNewCoefficients producing the wrong result? What would be a good way to produce the correct result?

def getNumbers(polynomial):
    regex = re.compile(r"[+-]?\d+(?:\.\d+)?")
    return  regex.findall(polynomial)

def formatNumbers(numbers):
    formattedNumbers = []
    for e in numbers:
        if (e[0] == '+'):
            formattedNumbers.append(e[1:])
        else:
            formattedNumbers.append(e)
    return formattedNumbers

def getNumberPositions(polynomial, numbers):
    numberPositions =  []
    for e in numbers:
        tmp = [m.start() for m in re.finditer(e, polynomial)]  
        for f in tmp:
           if f not in numberPositions:
                numberPositions.append(f)
    return sorted(numberPositions)

def getNewCoefficients(polynomial, numberPositions, numbers):
    tmp = '0'
    newCoefficients = []
    for i in range(0,len(numberPositions)):
        if numberPositions[i] + 1 < len(polynomial):
            if polynomial[numberPositions[i] + 1] == '+' or polynomial[numberPositions[i] + 1] == '-':
                newCoefficients.append(int(numbers[i])*int(tmp))
        elif numberPositions[i] - 1 > 0:
            if polynomial[numberPositions[i] - 1] == '+' or polynomial[numberPositions[i] - 1] == '-':
                newCoefficients.append(int(numbers[i]))
        tmp = numbers[i]
    return newCoefficients
Nutcracko
  • 23
  • 1
  • 5
  • Looks like a math question. Fwiw, I don't see `getNumbers` being called where that contains the only regex I see. Also, this text `2x^3+4x^2+8x-16` is not in a suitable form to represent an equation to a computer algorithm. –  Jun 18 '18 at 17:58
  • Seems to me that if you use that regex to get the numbers, and then depend on the resulting array that you'll get `['2', '3', '4', '2', '8', '-16']`. Wouldn't that represent `2x^3+4x^2+8x^-16` ? – LukStorms Jun 18 '18 at 18:24
  • Does it account for if there is no explicit exponent, like `+8x` (since that's the part that's missing)? – Edward Minnix Jun 18 '18 at 19:17

2 Answers2

1

This problem can be solved much more easily using negative lookbehind assertions.

COEFFICIENT_PATTERN = re.compile(r"(?<!x\^)(?<!x\^-)-?\d+")
coefficients = [int(c) for c in COEFFICIENT_PATTERN.findall(polynomial)]

This solution uses two negative lookbehinds for x^ and x^- (because look behinds must be fixed length). So it reads "get me all integers not preceded by x^.

>>> COEFFICIENT_PATTERN.findall('2x^3+4x^2+8x-16')
['2', '4', '8', '-16']
Edward Minnix
  • 2,889
  • 1
  • 13
  • 26
0

You don't need the re module, you can use the sympy module, but you have to download it first from http://www.sympy.org/en/index.html. I'm not a sympy expert, but I found a similar Stack Overflow question that may help you.

And, just to let you know, exponents does not count as coefficients...